0

Imagine I have a class like so:

public class MyClass {
    public string MyString { get;set; }
}

And then I extend this like so:

public class MyExtender : MyClass {
    public string MyString2 { get;set; }
}

How can I convert a MyClass into the extended one, I tried:

item = (MyExtender)classitem;

But this didn't work.

4

1 回答 1

1

最后,我没有找到转换它的方法,而是找到了一种将所有属性复制到扩展类的方法:

MyClass orig = GetItem(x);
MyExtender copy = new MyExtender();
PropertyInfo[] infos = typeof(MyClass).GetProperties();
foreach (PropertyInfo info in infos)
{
    info.SetValue(copy, info.GetValue(orig, null), null);
}       
于 2012-09-18T13:38:51.183 回答