This is an add-in for Fody
Generate copy constructors and extension methods to create a new instance with deep copy of properties.
See Wiki
See also Fody usage.
Install the DeepCopy.Fody NuGet package and update the Fody NuGet package:
PM> Install-Package Fody
PM> Install-Package DeepCopy.Fody
The Install-Package Fody
is required since NuGet always defaults to the oldest, and most buggy, version of any dependency.
Add <DeepCopy/>
to FodyWeavers.xml
<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
<DeepCopy/>
</Weavers>
public static class MyStaticClass
{
[DeepCopyExtension]
public static SomeObject DeepCopy(SomeObject source) => source;
}
public class SomeObject
{
public int Integer { get; set; }
public SomeEnum Enum { get; set; }
public DateTime DateTime { get; set; }
public string String { get; set; }
public IList<SomeObject> List { get; set; }
public IDictionary<SomeKey, SomeObject> Dictionary { get; set; }
}
public static class MyStaticClass
{
public static SomeObject DeepCopy(SomeObject source)
{
return source != null ? new SomeObject(source) : (SomeObject) null;
}
}
public class SomeObject
{
public SomeObject() { }
public SomeObject(SomeObject obj)
{
this.Integer = obj.Integer;
this.Enum = obj.Enum;
this.DateTime = obj.DateTime;
this.String = obj.String != null ? string.Copy(obj.String) : (string) null;
if (obj.List != null) {
var list = new System.Collections.Generic.List<SomeObject>();
foreach (var item in obj.List)
{
SomeObject someObject = ClassWithDeepCopyExtension.CopySomeObject(item);
list.Add(someObject);
}
this.List = list;
}
if (obj.Dictionary != null) {
var dictionary = new System.Collections.Generic.Dictionary<SomeKey, SomeObject>();
foreach (var keyValuePair in obj0.Dictionary)
{
SomeObject someObject = ClassWithDeepCopyExtension.CopySomeObject(keyValuePair.Value);
dictionary[keyValuePair.Key] = someObject;
}
this.Dictionary = dictionary;
}
}
public int Integer { get; set; }
public SomeEnum Enum { get; set; }
public DateTime DateTime { get; set; }
public string String { get; set; }
public IList<SomeObject> List { get; set; }
public IDictionary<SomeKey, SomeObject> Dictionary { get; set; }
}
Icon copy by projecthayat of The Noun Project