Skip to content

Inject deep copy constructors into C# types

License

Notifications You must be signed in to change notification settings

s-krawczyk/DeepCopy

 
 

Repository files navigation

Build status nuget

This is an add-in for Fody

Icon

Generate copy constructors and extension methods to create a new instance with deep copy of properties.

Usage

See Wiki

See also Fody usage.

NuGet installation

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 to FodyWeavers.xml

Add <DeepCopy/> to FodyWeavers.xml

<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
  <DeepCopy/>
</Weavers>

Sample

Your Code

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; }
}

What gets compiled

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

Icon copy by projecthayat of The Noun Project

About

Inject deep copy constructors into C# types

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%