Collections - greuelpirat/DeepCopy GitHub Wiki

Properties of IList<>, ISet<> and IDictionary<,> and their derivations are supported.

  • If implementation provides a copy constructor it will be used. Parameter of copy constructor must be the same type. For example the constructor List<T>(IEnumerable<T>) is not accepted as copy constructor.
  • Otherwise implementations must provide a default constructor
  • New instance for IList<> will be List<>
  • New instance for ISet<> will be HashSet<>
  • New instance for IDictionary<,> will be Dictionary<,>
  • Collection must be parameterized with copyable types.
  • Collection must provide a default constructor.

IList<>

Your property

public class SomeClass {
    public IList<SomeObject> List { get; set; }
}

What gets compiled

if (obj.List != null)
{
    this.List = (IList<SomeObject>) new System.Collections.Generic.List<SomeObject>();
    for (int index = 0; index < obj.List.Count; ++index)
        this.List.Add(obj.List[index] != null ? new SomeObject(obj.List[index]) : null);
}

ISet<>

Your property

public class SomeClass {
    public ISet<SomeObject> Set { get; set; }
}

What gets compiled

if (obj.Set != null)
{
    this.Set = (ISet<SomeObject>) new System.Collections.Generic.HashSet<SomeObject>();
    foreach (SomeObject o in (IEnumerable<SomeObject>) obj.Set)
        this.Set.Add(o != null ? new SomeObject(o) : (SomeObject) null);
}

IDictionary<,>

Your property

public class SomeClass {
    public IDictionary<SomeKey, SomeObject> Dictionary { get; set; }
}

What gets compiled

if (obj.Dictionary != null)
{
    this.Dictionary = (IDictionary<SomeKey, SomeObject>) new System.Collections.Generic.Dictionary<SomeKey, SomeObject>();
    foreach (KeyValuePair<SomeKey, SomeObject> pair in (IEnumerable<KeyValuePair<SomeKey, SomeObject>>) obj.Dictionary)
        this.Dictionary[new SomeKey(pair.Key)] = pair.Value != null ? new SomeObject(pair.Value) : (SomeObject) null;
}
⚠️ **GitHub.com Fallback** ⚠️