Skip to content

Custom properties

Oliver Ehrenmüller edited this page Aug 31, 2019 · 2 revisions

If you have a property that requires extra logic, you can inject copy instruction of remaining properties

  • Mark copy constructor with InjectDeepCopy
  • Mark special property with IgnoreDuringDeepCopy
  • Copy-Instructions are added before your code.

Your Code

public class ClassWithCopyConstructor
{
    public int Integer { get; set; }
    public IList<int> Integers { get; set; }

    [IgnoreDuringDeepCopy]
    public SpecialClass SpecialObject { get; set; }

    public ClassWithCopyConstructor() { }
    
    [InjectDeepCopy]
    public ClassWithCopyConstructor(ClassWithCopyConstructor source)
    {
        SpecialObject = SpecialLogic.SpecialCopy(source.SpecialObject);
    }
}

What get's compiled

public class ClassWithCopyConstructor
{
    public int Integer { get; set; }
    public IList<int> Integers { get; set; }
    public SpecialClass SpecialObject { get; set; }

    public ClassWithCopyConstructor() { }
    public ClassWithCopyConstructor(ClassWithCopyConstructor source)
    {
       this.Integer = source.Integer;
       if (source.Integers != null)
       {
           this.Integers = (IList<int>) new List<int>();
           for (int index = 0; index < source.Integers.Count; ++index)
               this.Integers.Add(source.Integers[index]);
        }
        SpecialObject = SpecialLogic.SpecialCopy(source.SpecialObject);
    }
}
Clone this wiki locally