在C#中将Parent对象强制转换为Child对象
您好我想在C#中将Parent对象强制转换为Child对象
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 lstParent; List lstChild = new List(); foreach(var obj in lstParent) { lstChild.add((Child)obj); }
作为子类inheritance父类,所以子类已经有父类成员我只想自动填充它们,以便我可以填充子类的datamember
如果我理解你的“我只想自动填充它们”注释正确,你想要创建一个新的Child对象,其中填充了Parent的值,并使用新属性的默认值。 最好的方法是创建一个复制值的构造函数:
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;} public Child (Parent parentToCopy) { this.FirstName = parentToCopy.FirstName; this.LastName = parentToCopy.LastName; this.City = parentToCopy.City; this.PhoneNumber = string.Empty; // Or any other default. this.MobileNumber = string.Empty; } }
现在,您可以像上面的答案一样使用LINQ来创建每个Parent的Child:
List lstChild = lstParent.Select(parent => new Child(parent)).ToList();
请注意,这与@daryal的答案非常相似,但将父对象复制逻辑包装在构造函数中,而不是在new Child()
调用中将其置于外部。
我这样做( 这只是一个例子 ):
using System.Reflection; public class DefaultObject { ... } public class ExtendedObject : DefaultObject { .... public DefaultObject Parent { get; set; } public ExtendedObject() {} public ExtendedObject(DefaultObject parent) { Parent = parent; foreach (PropertyInfo prop in parent.GetType().GetProperties()) GetType().GetProperty(prop.Name).SetValue(this, prop.GetValue(parent, null), null); } }
使用:
DefaultObject default = new DefaultObject { /* propery initialization */ }; ExtendedObject extended = new ExtendedObject(default); // now all properties of extended are initialized by values of default properties. MessageBox.Show(extended.Parent.ToString()); // now you can get reference to parent object
我喜欢这样:
class Parent { ... } class Child :Parent { ... public Child(Parent p) { foreach (FieldInfo prop in p.GetType().GetFields()) GetType().GetField(prop.Name).SetValue(this, prop.GetValue( p)); foreach (PropertyInfo prop in p.GetType().GetProperties()) GetType().GetProperty(prop.Name).SetValue(this, prop.GetValue( p, null), null); } }
这就是我提出的解决方案。
public static void ShallowConvert(this T parent, U child) { foreach (PropertyInfo property in parent.GetType().GetProperties()) { if (property.CanWrite) { property.SetValue(child, property.GetValue(parent, null), null); } } }
您也可以使用reflection,但这对您的情况来说更简单。
foreach(var obj in lstParent) { Child child = new Child(){ FirstName = obj.FirstName, LastName=obj.LastName, City = obj.City}; child.MobileNumber = "some mobile number"; child.PhoneNumber = "some phone number"; lstChild.Add((Child)obj); }
var lstChild = lstParent.Cast().ToList();
要么
var lstChild = lstParent.ConvertAll(x=>(Child)x);
但是,这两者都假设Parent
列表实际上包含Child
实例。 您无法更改对象的实际类型。
Downcast是一个术语,用于描述将父类的对象强制转换为子类。
这不是最好的方法,但是如果需要快速解决方案,可以先尝试将父对象转换为’object’类型,然后将对象转换为子类型。
Parent p = new Parent(); Child c = (Child)((object) p);