星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DataConvertCommon.cs 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Common.system
  6. {
  7. /// <summary>
  8. /// 数据转换帮助类
  9. /// 创建人:赵耀
  10. /// 创建时间:2018年10月24日
  11. /// </summary>
  12. public class DataConvertCommon
  13. {
  14. /// <summary>
  15. /// 父类向子类赋值
  16. /// 创建人:赵耀
  17. /// 创建时间:2018年10月24日
  18. /// </summary>
  19. /// <typeparam name="TParent">父类</typeparam>
  20. /// <typeparam name="TChild">子类</typeparam>
  21. /// <param name="parent">子数据</param>
  22. /// <returns></returns>
  23. public static TChild ParentClassCopy<TParent, TChild>(TParent parent) where TChild : TParent, new()
  24. {
  25. TChild child = new TChild();
  26. var ParentType = typeof(TParent);
  27. var Properties = ParentType.GetProperties();
  28. foreach (var Propertie in Properties)
  29. {
  30. //循环遍历属性
  31. if (Propertie.CanRead && Propertie.CanWrite)
  32. {
  33. //进行属性拷贝
  34. Propertie.SetValue(child, Propertie.GetValue(parent, null), null);
  35. }
  36. }
  37. return child;
  38. }
  39. }
  40. }