ModelToStr.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. namespace CommonTool
  5. {
  6. public class ModelToStr
  7. {
  8. /// <summary>
  9. /// 把实体对象的属性与值变成字符串
  10. /// </summary>
  11. /// <param name="model">实体对象</param>
  12. /// <returns></returns>
  13. public static string ToModelStr<T>(T model)
  14. where T : class, new()
  15. {
  16. if (model == null) return "";
  17. string sb = "";
  18. Type type = model.GetType();
  19. PropertyInfo[] propertys = type.GetProperties();
  20. foreach (PropertyInfo prop in propertys)
  21. {
  22. PropertyInfo property = type.GetProperty(prop.Name);
  23. object val = property.GetValue(model, null);
  24. string value = val?.ToString().Trim() ?? "";
  25. sb += prop.Name + '-' + value + '|';
  26. }
  27. if (sb.Length <= 0) return sb;
  28. sb = sb.Substring(0, sb.Length - 1);
  29. return sb;
  30. }
  31. /// <summary>
  32. /// 把实体对象的属性与值变成字符串
  33. /// </summary>
  34. /// <returns></returns>
  35. public static T ToStrModel<T>(string modelStr)
  36. where T : class, new()
  37. {
  38. if (modelStr == null)
  39. return null;
  40. T model=new T();
  41. Type type = model.GetType();
  42. Dictionary<string,object> dict=new Dictionary<string, object>();
  43. PropertyInfo[] propertys = type.GetProperties();
  44. string[] objs = modelStr.Split('|');
  45. foreach (string obj in objs)
  46. {
  47. string[] str = obj.Split('-');
  48. dict[str[0]] = str[1];
  49. }
  50. foreach (PropertyInfo prop in propertys)
  51. {
  52. string propType = prop.PropertyType.ToString().ToLower();
  53. if (dict[prop.Name].ToString() == "")
  54. dict[prop.Name] = null;
  55. PropertyInfo property = type.GetProperty(prop.Name);
  56. object value = null;
  57. if (propType.Contains("decimal"))
  58. {
  59. if (dict[prop.Name] != null)
  60. value = Convert.ToDecimal(dict[prop.Name]);
  61. }
  62. else if (propType.Contains("decimal"))
  63. {
  64. if (dict[prop.Name] != null)
  65. value = Convert.ToDecimal(dict[prop.Name]);
  66. }
  67. else if (propType.Contains("int32"))
  68. {
  69. if (dict[prop.Name] != null)
  70. value = Convert.ToInt32(dict[prop.Name]);
  71. }
  72. else if (propType.Contains("int64"))
  73. {
  74. if (dict[prop.Name] != null)
  75. value = Convert.ToInt32(dict[prop.Name]);
  76. }
  77. else if (propType.Contains("datetime"))
  78. {
  79. if (dict[prop.Name] != null)
  80. value = Convert.ToDateTime(dict[prop.Name]);
  81. }
  82. else
  83. {
  84. value = dict[prop.Name];
  85. }
  86. property.SetValue(model,value,null);
  87. }
  88. return model;
  89. }
  90. /// <summary>
  91. /// 把实体对象的属性与值转成字符串再转成实体对象
  92. /// </summary>
  93. /// <param name="model">实体对象</param>
  94. /// <returns></returns>
  95. public static T ModelStrModel<T>(T model)
  96. where T : class, new()
  97. {
  98. if (model == null)
  99. return null;
  100. return ToStrModel<T>(ToModelStr(model));
  101. }
  102. }
  103. }