星火批注
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.2KB

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