小编dav*_*das的帖子

c# - inheritence + reflection - 如何不克隆对象

我发现以下代码片段让我感到困惑.

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... 
}
Run Code Online (Sandbox Code Playgroud)

我的主要问题是......为什么有人会想到这样做......这段代码可以带来什么好处.

c# reflection inheritance

-2
推荐指数
1
解决办法
65
查看次数

标签 统计

c# ×1

inheritance ×1

reflection ×1