![](/img/trans.png)
[英]C# Reflection: How to get the properties of the derived class from the base class
[英]Copy properties from base class to derived class without manually mapping in C# with reflection
如何将属性从基类实例复制到派生类?
public class Base
{
public string BaseProperty { get; set; }
}
public class House : Base
{
public string HouseProp { get; set; }
}
我不想这样做:
Base base = new Base() { BaseProperty = "Hello World" };
House house = new House();
house.BaseProperty = base.BaseProperty;
Base _base = new Base() { BaseProperty = "Hello World" };
House _house = new House(_base);
public class Base
{
public string BaseProperty { get; set; }
}
public class House : Base
{
public House(Base _base)
{
foreach (PropertyInfo property in _base.GetType().GetProperties())
{
PropertyInfo propinfo = _base.GetType().GetProperty(property.Name);
propinfo.SetValue(this, property.GetValue(_base, null), null);
}
}
public string HouseProp { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.