遍历父类的属性赋值给子类的方法:
- private static ChildClass AutoCopy(ParentClass parent)
- {
- ChildClass child = new ChildClass();
- var ParentType = typeof(ParentClass);
- var Properties = ParentType.GetProperties();
- foreach (var Propertie in Properties)
- {
- if (Propertie.CanRead && Propertie.CanWrite)
- {
- //通过Propertie.PropertieType==typeof(AnyClass)成员的类型,
- //Propertie.Name=="AnyName"成员的名称,可以达到更准确的控制
- Propertie.SetValue(child, Propertie.GetValue(parent, null), null);
- }
- }
- return child;
- }