通用 C# 复制构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/433926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:13:59  来源:igfitidea点击:

Generic C# Copy Constructor

c#constructorcopy

提问by lomaxx

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 properties, but I'm wondering if there's a better way?

为我的 c# 类编写通用复制构造函数的最佳方法是什么?它们都继承自一个抽象基类,因此我可以使用反射来映射属性,但我想知道是否有更好的方法?

采纳答案by Marc Gravell

You can create a shallow copy efficientlywith reflection by pre-compiling it, for example with Expression. For example, like so.

您可以创建一个浅拷贝高效与反思通过预编译它,例如使用Expression。例如,像这样

For deep copies, serialization is the most reliable approach.

对于深拷贝,序列化是最可靠的方法。

回答by ?yvind Skaar

Avoid reflection if you can. Each class should have the responsibility of copying its own properties, and send it further to the base method.

如果可以,请避免反射。每个类都应该负责复制自己的属性,并将其进一步发送给基方法。

回答by Vordreller

A copy constructor basically means you have a single parameter, which is the object you're going to copy.

复制构造函数基本上意味着您只有一个参数,即您要复制的对象。

Also, do a deep copy, not a shallow copy.

另外,做深拷贝,而不是浅拷贝。

If you don't know what deep and shallow copies are, then here's the deal:

如果你不知道深拷贝和浅拷贝是什么,那么这就是交易:

Suppose you're copying a class that has a single row of integers as field.

假设您正在复制一个具有单行整数作为字段的类。

A shallow copy would be:

浅拷贝将是:

public class Myclass()
{
    private int[] row;
    public MyClass(MyClass class)
    {
        this.row = class.row
    }
}

deep copy is:

深拷贝是:

public class Myclass()
{
    private int[] row;
    public MyClass(MyClass class)
    {
        for(int i = 0; i<class.row.Length;i++)
        {
            this.row[i] = class.row[i];
        }
    }
}

A deep copy really gets the actuall values and puts them in a new field of the new object, whilst a shallow copy only copies the pointers.

深拷贝真正获取实际值并将它们放入新对象的新字段中,而浅拷贝仅复制指针。

With the shallow copy, if you set:

使用浅拷贝,如果你设置:

row[3] = 5;

And then print both rows, both prints will have 5 as value of the 4th number. With a deep copy however, only the first print will have this, since the rows don't have the same pointers.

然后打印两行,两个打印的第 4 个数字的值都是 5。然而,对于深拷贝,只有第一次打印才会有这个,因为行没有相同的指针。

回答by B2K

Here's a constructor that I'm using. Note that this is a shallow constructor, and rather simplistic, due to the nature of my base class. Should be good enough to get you started.

这是我正在使用的构造函数。请注意,由于我的基类的性质,这是一个浅层构造函数,而且相当简单。应该足以让你开始。

public partial class LocationView : Location
{
    public LocationView() {}

    // base class copy constructor
    public LocationView(Location value) {
        Type t = typeof(Location);
        PropertyInfo[] properties = t.GetProperties();
        foreach (PropertyInfo pi in properties)
        {
            pi.SetValue(this, pi.GetValue(value, null), null);
        }
    }
    public Quote Quote { get; set; }
}

回答by Nikita Ilin

You may reference valueinjecter and fasterflect nuget packages and use:

您可以参考 valueinjecter 和 fastflect nuget 包并使用:

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