EntityClone.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace HXX.Scanner.Common
  7. {
  8. public class EntityClone
  9. {
  10. /// <summary>
  11. /// 可父类向子类拷贝
  12. /// </summary>
  13. /// <typeparam name="Tp"></typeparam>
  14. /// <typeparam name="Tc"></typeparam>
  15. /// <param name="parent"></param>
  16. /// <returns></returns>
  17. public static Tc DeriveCopy<Tp, Tc>(Tp parent) where Tc : new()
  18. {
  19. try
  20. {
  21. Tc child = new Tc();
  22. var ParentType = typeof(Tp);
  23. var Properties = ParentType.GetProperties();
  24. foreach (var Propertie in Properties)
  25. {
  26. if (Propertie.CanRead && Propertie.CanWrite)
  27. {
  28. Propertie.SetValue(child, Propertie.GetValue(parent, null), null);
  29. }
  30. }
  31. var fields = ParentType.GetFields();
  32. foreach (var f in fields)
  33. {
  34. f.SetValue(child, f.GetValue(parent));
  35. }
  36. return child;
  37. }
  38. catch (Exception)
  39. {
  40. throw;
  41. }
  42. }
  43. }
  44. }