Generic C# Copy Constructor

前端 未结 5 1391
再見小時候
再見小時候 2021-01-31 10:55

What would be the best way to write a generic copy constructor function for my c# classes? They all inherit from an abstract base class so I could use reflection to map the prop

5条回答
  •  面向向阳花
    2021-01-31 11:13

    You may reference valueinjecter and fasterflect nuget packages and use:

    public class Myclass()
    {
        private string _property;
        public MyClass(MyClass obj)
        {
             this.InjectFrom(obj.DeepClone());
        }
    }
    

提交回复
热议问题