遍历父类的属性赋值给子类的方法:
- 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.SetValue(child, Propertie.GetValue(parent, null), null);
- }
- }
- return child;
- }
时间: 2024-10-28 11:32:56