嗨,我想在C#中将父对象转换为子对象。
public class Parent
{
public string FirstName {get; set;}
public string LastName {get; set;}
public string City {get; set;}
}
public class Child : Parent
{
public string PhoneNumber {get; set;}
public string MobileNumber {get; set;}
}
现在的情况是有一个父对象列表,我想生成一个子对象列表,以便我可以获得更多的扩展信息。
List<Parent> lstParent;
List<Child> lstChild = new List<Child>();
foreach(var obj in lstParent)
{
lstChild.add((Child)obj);
}
作为子类继承了父类,因此子类已经拥有了父类成员。我只想自动填充它们,以便可以填充子类的datamember。
prop.SetValue(this, prop.GetValue(parent));
呢? - Andrewprop
(由parent.GetType().GetProperties()
返回的数组的PropertyInfo
元素)如何与GetType().GetProperty(prop.Name)
(使用prop
的Name
查找的PropertyInfo
)有所不同,这让我对为什么在这里使用冗长版本感到困惑。 - zcoop98