[英]c# - inheritence + reflection - how not to clone an object
I found the following code snippet which puzzled me.我发现以下代码片段让我感到困惑。
public class Bclass : Aclass
{
public const BindingFlags Flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
public Bclass(IAclass a) : base(string.Empty)
{
var destFields = this.GetType().BaseType.GetFields( Flags );
a.GetType().GetFields( Flags ).Where(x => destFields.Contains(x)).ToList().ForEach(property =>
{
destFields.First(x => x == property).SetValue(this, property.GetValue(a));
});
var destProperties = this.GetType().BaseType.GetProperties( Flags );
a.GetType().GetProperties( Flags ).Where(x => destProperties.Contains(x)).ToList().ForEach(property =>
{
destProperties.First(x => x == property).SetValue(this, property.GetValue(a, null));
});
}
// some more methods...
}
My main Q is.... why would anyone think of doing that... What benefit(s) can come out of this code.我的主要问题是......为什么会有人想到这样做......这段代码可以带来什么好处。
What it does: memberwise clone from a
into the current newly created instance它的作用:从
a
成员方式克隆到当前新创建的实例中
Advantages:好处:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.