ToModel.cs 877 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Reflection;
  7. namespace CP.Common
  8. {
  9. /// <summary>
  10. /// 实体类转换
  11. /// </summary>
  12. public class ToModel
  13. {
  14. public static R Mapping<R, T>(T model)
  15. {
  16. R result = Activator.CreateInstance<R>();
  17. foreach (PropertyInfo info in typeof(R).GetProperties())
  18. {
  19. PropertyInfo pro = typeof(T).GetProperty(info.Name);
  20. if (pro != null)
  21. {
  22. try
  23. {
  24. info.SetValue(result, pro.GetValue(model));
  25. }
  26. catch (Exception)
  27. {
  28. }
  29. }
  30. }
  31. return result;
  32. }
  33. }
  34. }