遍历父类的属性赋值给子类的方法:

    1. private static ChildClass AutoCopy(ParentClass parent)
    2. {
    3. ChildClass child = new ChildClass();
    4. var ParentType = typeof(ParentClass);
    5. var Properties = ParentType.GetProperties();
    6. foreach (var Propertie in Properties)
    7. {
    8. if (Propertie.CanRead && Propertie.CanWrite)
    9. {
    10. //通过Propertie.PropertieType==typeof(AnyClass)成员的类型,
    11. //Propertie.Name=="AnyName"成员的名称,可以达到更准确的控制
    12. Propertie.SetValue(child, Propertie.GetValue(parent, null), null);
    13. }
    14. }
    15. return child;
    16. }
04-19 19:45
查看更多