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

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

猜你喜欢

转载自blog.csdn.net/backzero333/article/details/78247830