1234567891011121314151617181920212223242526272829303132333435363738394041 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- 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();
- 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;
- }
- }
- }
|