来自C ++背景,我发现克隆C#中的对象有点难以习惯。为了澄清我的一些困惑,我正在寻找一种优雅的方法来将基类型的对象克隆到派生类型。
举例说明:
public class Base
{
public string Member1;
public int Member2;
public float Member3;
public bool Member4;
}
public class Derived : Base
{
public List<Base> Children;
}
Base base = new Base();
并且我想在创建Base对象的成员副本时创建一个“Derived”实例 - 最好不要手动分配它们。
注意:也许这更适合值类型?
答案 0 :(得分:4)
由于您无法更改对象的类型,因此您有以下几种选择:
Base
Base
Base
复制属性
对于后者,MiscUtil有一个有用的工具:
Base b= ...
Derived item = PropertyCopy<Derived>.CopyFrom(b);
封装:
public class Derived
{
readonly Base b;
public Derived(Base b) {this.b=b;}
public List<Base> Children;
public string Member1 {get {return b.Member1;} set {...} }
public int Member2 {etc}
public float Member3 {etc}
public bool Member4 {etc}
}
或作为手册:
public class Derived : Base
{
public Derived(Base b) {
this.Member1 = b.Member1;
// etc
}
// additional members...
}
或(评论)获得复制自己的基础:
public class Derived : Base
{
public Derived(Base b) : base(b) { }
// additional members...
}
public class Base
{
// members not shown...
public Base() {}
protected Base(Base b) {
this.Member1 = b.Member1;
// etc
}
// additional members...
}
(其中Base
的构造函数初始化Base
)
答案 1 :(得分:2)
/// Clone all fields from an instance of base class TSrc into derived class TDst
public static TDst Clone<TSrc, TDst>(TSrc source, TDst target)
where TDst : TSrc
{
var bf = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;
foreach (FieldInfo fis in source.GetType().GetFields(bf))
fis.SetValue(target, fis.GetValue(source));
return target;
}
/// Create a new instance of a derived class, cloning all fields from type TSrc
public static TDst Clone<TSrc, TDst>(TSrc source)
where TDst : TSrc, new()
{
return Clone(source, new TDst());
}