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.

141 lines
4.8KB

  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 GpsCardGatewayPosition.Model.Context
  8. {
  9. public abstract class ContextBase
  10. {
  11. /// <summary>
  12. /// 反序列化 将 属性名:属性值 AND 属性名=属性值 字符串反序列化为对象
  13. /// </summary>
  14. /// <param name="val"></param>
  15. public void Deserlize(string val)
  16. {
  17. if (string.IsNullOrEmpty(val)) return;
  18. var items = val.Split(new[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
  19. foreach (var item in items)
  20. {
  21. var kv = item.Split('=');
  22. if (kv.Length == 2)
  23. SetValue(this, kv[0], kv[1]);
  24. }
  25. }
  26. /// <summary>
  27. /// 重写 ToString 方法,将所有属性拼接成 属性名=属性值 AND 属性名=属性值 格式的字符串
  28. /// </summary>
  29. /// <returns></returns>
  30. public override string ToString()
  31. {
  32. var tStr = string.Empty;
  33. var properties = GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
  34. foreach (var item in properties)
  35. {
  36. var name = item.Name;
  37. var value = item.GetValue(this, null);
  38. if (value != null)
  39. {
  40. if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
  41. {
  42. // Skipped insecurity
  43. tStr += string.Format("{0}={1}&", name, value);
  44. }
  45. }
  46. }
  47. return tStr.TrimEnd('&').ToLower();
  48. }
  49. /// <summary>
  50. /// 类型匹配
  51. /// </summary>
  52. /// <param name="type"></param>
  53. /// <param name="typeName"></param>
  54. /// <returns></returns>
  55. public static bool IsType(Type type, string typeName)
  56. {
  57. if (type.ToString() == typeName)
  58. return true;
  59. return type.ToString() != "System.Object" && IsType(type.BaseType, typeName);
  60. }
  61. /// <summary>
  62. /// 给类的属性设置值
  63. /// </summary>
  64. /// <param name="entity"></param>
  65. /// <param name="fieldName"></param>
  66. /// <param name="fieldValue"></param>
  67. public static void SetValue(object entity, string fieldName, string fieldValue)
  68. {
  69. var entityType = entity.GetType();
  70. var propertyInfo = entityType.GetProperty(fieldName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
  71. if (propertyInfo == null) return;
  72. if (IsType(propertyInfo.PropertyType, "System.String"))
  73. {
  74. propertyInfo.SetValue(entity, fieldValue, null);
  75. }
  76. if (IsType(propertyInfo.PropertyType, "System.Boolean"))
  77. {
  78. propertyInfo.SetValue(entity, Boolean.Parse(fieldValue), null);
  79. }
  80. if (IsType(propertyInfo.PropertyType, "System.Nullable`1[System.Boolean]"))
  81. {
  82. propertyInfo.SetValue(entity, fieldValue != "" && Boolean.Parse(fieldValue), null);
  83. }
  84. if (IsType(propertyInfo.PropertyType, "System.Int32"))
  85. {
  86. propertyInfo.SetValue(entity, fieldValue != "" ? int.Parse(fieldValue) : 0, null);
  87. }
  88. if (IsType(propertyInfo.PropertyType, "System.Nullable`1[System.Int32]"))
  89. {
  90. propertyInfo.SetValue(entity, fieldValue != "" ? int.Parse(fieldValue) : 0, null);
  91. }
  92. if (IsType(propertyInfo.PropertyType, "System.Decimal"))
  93. {
  94. try
  95. {
  96. propertyInfo.SetValue(entity, fieldValue != "" ? Decimal.Parse(fieldValue) : new Decimal(0), null);
  97. }
  98. catch (Exception)
  99. {
  100. propertyInfo.SetValue(entity, new Decimal(0), null);
  101. }
  102. }
  103. if (IsType(propertyInfo.PropertyType, "System.Nullable`1[System.DateTime]"))
  104. {
  105. if (fieldValue != "")
  106. {
  107. try
  108. {
  109. propertyInfo.SetValue(
  110. entity,
  111. (DateTime?)DateTime.ParseExact(fieldValue, "yyyy-MM-dd HH:mm:ss", null), null);
  112. }
  113. catch
  114. {
  115. propertyInfo.SetValue(entity, (DateTime?)DateTime.ParseExact(fieldValue, "yyyy-MM-dd", null), null);
  116. }
  117. }
  118. else
  119. propertyInfo.SetValue(entity, null, null);
  120. }
  121. }
  122. }
  123. }