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.

70 lines
2.0 KiB

3 weeks ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace NFine.Application.DHAY
  8. {
  9. public static class ConvertExt
  10. {
  11. #region 文本转换
  12. public static string ToStringExt(this object data)
  13. {
  14. return data == null ? string.Empty : data.ToString();
  15. }
  16. public static bool IsNullOrEmpty(this string str)
  17. {
  18. return string.IsNullOrEmpty(str);
  19. }
  20. //public static string ToStringExt(this IConvertHelper convert, object data)
  21. //{
  22. // return ToStringExt(data);
  23. //}
  24. #endregion
  25. public static To Mapping<From, To>(From model)
  26. {
  27. To result = Activator.CreateInstance<To>();
  28. foreach (PropertyInfo info in typeof(To).GetProperties())
  29. {
  30. PropertyInfo pro = typeof(From).GetProperty(info.Name);
  31. if (pro != null && pro.PropertyType == info.PropertyType)
  32. info.SetValue(result, pro.GetValue(model));
  33. }
  34. return result;
  35. }
  36. public static To Mapping<From, To>(From model, To result)
  37. {
  38. foreach (PropertyInfo info in typeof(To).GetProperties())
  39. {
  40. PropertyInfo pro = typeof(From).GetProperty(info.Name);
  41. if (pro != null && pro.PropertyType == info.PropertyType)
  42. {
  43. var value = pro.GetValue(model);
  44. if (value == null)
  45. {
  46. continue;
  47. }
  48. info.SetValue(result, value);
  49. }
  50. }
  51. return result;
  52. }
  53. }
  54. public static class CommonHelper
  55. {
  56. public static string GetBaseUrl(Uri url)
  57. {
  58. return new StringBuilder()
  59. .Append(url.Scheme)
  60. .Append("://")
  61. .Append(url.Authority)
  62. .ToString();
  63. }
  64. }
  65. }