UtilityVerse.Copy by pritom purkayasta
NuGet / site data
Details
Info
Name: UtilityVerse.Copy
A Roslyn source generator for generating shallow copy and deep copy for class, records and structs
Author: pritom purkayasta
NuGet: https://www.nuget.org/packages/UtilityVerse.Copy/
You can find more details at https://github.com/purkayasta/TheUtilityVerse
Author
pritom purkayasta
Original Readme
The UtilityVerse 🧰
(Multi-verse of utility methods) — a library full of helper methods you might want to use.
⭐️ Give it a star if you like the project!
📦 Packages in This Universe
Use whatever suits your project:
-
UtilityVerse ->
🧰 No dependencies — includes core utility methods.
-
UtilityVerse.ASPNET ->
🌐 ASP.NET Core-specific helpers and extensions.
-
UtilityVerse.Copy ->
✍️ A Roslyn-based source generator that automatically generates
ShallowCopy()
andDeepCopy()
methods for your models.
→ See full details here:UtilityVerse.Copy/README.md
To use this utility library, you may want to start with the Utility
class.
Made with ❤️ in C#.
About
Deep Clone and Shallow Copy of objects
How to use
Example (source csproj, source files)
- CSharp Project
- Program.cs
- Person.cs
This is the CSharp Project that references UtilityVerse.Copy
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="UtilityVerse.Copy" Version="0.5.0" />
</ItemGroup>
</Project>
This is the use of UtilityVerse.Copy in Program.cs
using CloneData;
Console.WriteLine("Hello, World!");
Person p = new ();
p.FirstName = "Andrei";
p.LastName = "Ignat";
p.Age = 54;
var p1=p.DeepCopy();
Console.WriteLine(p1.Name());
This is the use of UtilityVerse.Copy in Person.cs
using UtilityVerse.Copy;
namespace CloneData;
[DeepCopy]
public partial class Person
{
public string FirstName \{ get; set; \} = "";
public string LastName \{ get; set; \} = "";
public int Age \{ get; set; }
public string Name() => $"{FirstName} {LastName}";
public Person[] Childs \{ get; set; \} = [];
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- ClonableAttribute.g.cs
- CloneIgnoreAttribute.g.cs
- IClonable.g.cs
- Person.g.cs
- CloneData_Person_DeepCopy.g.cs
using System;
namespace Dolly
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class ClonableAttribute : Attribute
{
}
}
using System;
namespace Dolly
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class CloneIgnoreAttribute : Attribute
{
}
}
using System;
namespace Dolly
{
public interface IClonable<T> : ICloneable
{
T DeepClone();
T ShallowClone();
}
}
using Dolly;
using System.Linq;
namespace CloneData;
partial class Person : IClonable<Person>
{
object ICloneable.Clone() => this.DeepClone();
public virtual Person DeepClone() =>
new ()
{
FirstName = FirstName,
LastName = LastName,
Childs = Childs.Select(item => item.DeepClone()).ToArray()
};
public virtual Person ShallowClone() =>
new ()
{
FirstName = FirstName,
LastName = LastName,
Childs = Childs.ToArray()
};
}
// <auto-generated>
// This code was generated by Copy.
// Author: Pritom Purkayasta
// DO NOT modify this file manually. Changes may be overwritten.
// This file contains auto-generated DeepCopy() implementations.
// </auto-generated>
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Immutable;
using System.Collections.Concurrent;
using System.Collections.Frozen;
namespace CloneData
{
public partial class Person
{
[System.CodeDom.Compiler.GeneratedCode("DeepCopyGenerator", "1.0")]
public Person DeepCopy()
{
return new Person
{
FirstName = this.FirstName,
LastName = this.LastName,
Age = this.Age,
Childs = this.Childs?.Select(x => x?.DeepCopy()).ToArray(),
};
}
}
}
Useful
Download Example (.NET C#)
Share UtilityVerse.Copy
https://ignatandrei.github.io/RSCG_Examples/v2/docs/UtilityVerse.Copy
Category "Clone" has the following generators:
1 CopyTo
2 Dolly