123456789101112131415161718192021222324252627282930313233343536 |
- namespace Common.system
- {
- /// <summary>
- /// 数据转换帮助类
- /// 创建人:赵耀
- /// 创建时间:2018年10月24日
- /// </summary>
- public class DataConvertCommon
- {
- /// <summary>
- /// 父类向子类赋值
- /// 创建人:赵耀
- /// 创建时间:2018年10月24日
- /// </summary>
- /// <typeparam name="TParent">父类</typeparam>
- /// <typeparam name="TChild">子类</typeparam>
- /// <param name="parent">子数据</param>
- /// <returns></returns>
- public static TChild ParentClassCopy<TParent, TChild>(TParent parent) where TChild : TParent, new()
- {
- TChild child = new TChild();
- System.Type ParentType = typeof(TParent);
- System.Reflection.PropertyInfo[] Properties = ParentType.GetProperties();
- foreach (System.Reflection.PropertyInfo Propertie in Properties)
- {
- //循环遍历属性
- if (Propertie.CanRead && Propertie.CanWrite)
- {
- //进行属性拷贝
- Propertie.SetValue(child, Propertie.GetValue(parent, null), null);
- }
- }
- return child;
- }
- }
- }
|