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;
- }
- }
- }
|