public static TChild
AutoCopy<TParent, TChild>(TParent parent) where TChild
: TParent, new(){ TChild
child = new TChild(); var ParentType
= typeof(TParent); var Properties
= ParentType.GetProperties(); foreach (var Propertie in Properties) { //循环遍历属性 if (Propertie.CanRead
&& Propertie.CanWrite) { //进行属性拷贝 Propertie.SetValue(child,
Propertie.GetValue(parent, null), null); } } return child;
}
摘抄于http://www.cnblogs.com/Soar1991/archive/2012/11/04/2754319.html
本文介绍了一个实用的C#方法,该方法可以自动将一个类的所有属性复制到另一个类中,前提这两个类存在继承关系。通过反射机制实现了对属性的读取与设置,简化了属性复制的过程。
104

被折叠的 条评论
为什么被折叠?



