Skip to content

greuelpirat/DeepCopy

Repository files navigation

build 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

Source at SmokeTest\ReadMeSample.cs

Your Code

public class ReadMeSample
{
  public ReadMeSample() { }
  [DeepCopyConstructor] public ReadMeSample(ReadMeSample source) { }
  
  public int Integer { get; set; }
  public ReadMeEnum Enum { get; set; }
  public DateTime DateTime { get; set; }
  public string String { get; set; }
  public IList<ReadMeSample> List { get; set; }
  public IDictionary<ReadMeEnum, ReadMeSample> Dictionary { get; set; }
}

What gets compiled

public class ReadMeSample
{
  public ReadMeSample() { }

  public ReadMeSample(ReadMeSample source)
  {
    this.Integer = source.Integer;
    this.Enum = source.Enum;
    this.DateTime = source.DateTime;
    this.String = source.String != null ? new string(source.String.ToCharArray()) : (string)null;
    if (source.List != null)
    {
      IList<ReadMeSample> readMeSampleList = (IList<ReadMeSample>)new System.Collections.Generic.List<ReadMeSample>();
      foreach (ReadMeSample source1 in (IEnumerable<ReadMeSample>)source.List)
        readMeSampleList.Add(source1 != null ? new ReadMeSample(source1) : (ReadMeSample)null);
      this.List = readMeSampleList;
    }
    if (source.Dictionary == null)
      return;
    IDictionary<ReadMeEnum, ReadMeSample> dictionary = (IDictionary<ReadMeEnum, ReadMeSample>)new System.Collections.Generic.Dictionary<ReadMeEnum, ReadMeSample>();
    foreach (KeyValuePair<ReadMeEnum, ReadMeSample> keyValuePair in (IEnumerable<KeyValuePair<ReadMeEnum, ReadMeSample>>)source.Dictionary)
    {
      ReadMeEnum key = keyValuePair.Key;
      ReadMeSample readMeSample = keyValuePair.Value != null ? new ReadMeSample(keyValuePair.Value) : (ReadMeSample)null;
      dictionary[key] = readMeSample;
    }
    this.Dictionary = dictionary;
  }
  
  public int Integer { get; set; }
  public ReadMeEnum Enum { get; set; }
  public DateTime DateTime { get; set; }
  public string String { get; set; }
  public IList<ReadMeSample> List { get; set; }
  public IDictionary<ReadMeEnum, ReadMeSample> Dictionary { get; set; }
}

Decompiled with JetBrains dotPeek 2021.2.1 Build 212.0.20210826.112035

Icon

Icon copy by projecthayat of The Noun Project