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 beList<>
- New instance for
ISet<>
will beHashSet<>
- New instance for
IDictionary<,>
will beDictionary<,>
- Collection must be parameterized with copyable types.
- Collection must provide a default constructor.
public class SomeClass {
public IList<SomeObject> List { get; set; }
}
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);
}
public class SomeClass {
public ISet<SomeObject> Set { get; set; }
}
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);
}
public class SomeClass {
public IDictionary<SomeKey, SomeObject> Dictionary { get; set; }
}
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;
}