C# 将父类的值赋值到子类中

来源:互联网 发布:淘宝大悲咒水晶杯真假 编辑:程序博客网 时间:2024/06/10 13:04
public static TChild AutoCopy<TParent, TChild>(TParent parent) where TChild : TParent, new()
{
    TChild child = new TChild();
    var ParentType = typeof(TParent);
    var Properties = ParentType.GetProperties();
    foreach (var Propertie in Properties)
    {
        //循环遍历属性
        if (Propertie.CanRead && Propertie.CanWrite)
        {
            //进行属性拷贝
            Propertie.SetValue(child, Propertie.GetValue(parent, null), null);
        }
    }
    return child;

}

摘抄于http://www.cnblogs.com/Soar1991/archive/2012/11/04/2754319.html

原创粉丝点击