HelpTo.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Runtime;
  8. using System.Runtime.InteropServices;
  9. using System.Text.RegularExpressions;
  10. using System.Web;
  11. namespace SiwiFms.Helper
  12. {
  13. public static class HelpTo
  14. {
  15. /// <summary>
  16. /// 将对象转json字符串
  17. /// </summary>
  18. /// <param name="obj"></param>
  19. /// <returns></returns>
  20. public static string ToJson(this object obj)
  21. {
  22. return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
  23. }
  24. /// <summary>
  25. /// 将string Json字符串转实体类型
  26. /// </summary>
  27. /// <typeparam name="T"></typeparam>
  28. /// <param name="str"></param>
  29. /// <returns></returns>
  30. public static T ToEntity<T>(this string str)
  31. {
  32. return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(str);
  33. }
  34. /// <summary>
  35. /// 将对象转Int
  36. /// </summary>
  37. /// <param name="str"></param>
  38. /// <returns></returns>
  39. public static int ToInt(this object str)
  40. {
  41. if (str == null)
  42. return 0;
  43. else
  44. return Convert.ToInt32(str);
  45. }
  46. /// <summary>
  47. /// 将对象转double类型
  48. /// </summary>
  49. /// <param name="str"></param>
  50. /// <returns></returns>
  51. public static double ToDouble(this object str)
  52. {
  53. if (str == null)
  54. return 0;
  55. else
  56. {
  57. double tmp;
  58. bool bl= double.TryParse(str.ToStr(), out tmp);
  59. if (bl)
  60. return tmp;
  61. else
  62. return 0;
  63. }
  64. }
  65. /// <summary>
  66. /// 是否是Int类型
  67. /// </summary>
  68. /// <param name="obj"></param>
  69. /// <returns></returns>
  70. public static bool IsInt(this object obj)
  71. {
  72. int tmp;
  73. return int.TryParse(obj.ToString(), out tmp);
  74. }
  75. /// <summary>
  76. /// 将对象转string
  77. /// </summary>
  78. /// <param name="t"></param>
  79. /// <returns></returns>
  80. public static string ToStr(this object t)
  81. {
  82. if (t == null)
  83. return string.Empty;
  84. else
  85. return t.ToString();
  86. }
  87. /// <summary>
  88. /// 是否为空
  89. /// </summary>
  90. /// <param name="t"></param>
  91. /// <returns></returns>
  92. public static bool IsNull(this object t)
  93. {
  94. if (t == null)
  95. return true;
  96. else
  97. return false;
  98. }
  99. public static bool IsNullOrEmpty(this string str)
  100. {
  101. if (string.IsNullOrEmpty(str))
  102. return true;
  103. else
  104. return false;
  105. }
  106. /// <summary>
  107. /// 获取新的ID
  108. /// </summary>
  109. public static string NewId
  110. {
  111. get {
  112. return Guid.NewGuid().ToString();
  113. }
  114. }
  115. /// <summary>
  116. /// 获取新的时间
  117. /// </summary>
  118. public static DateTime NewDate
  119. {
  120. get {
  121. return DateTime.Now;
  122. }
  123. }
  124. /// <summary>
  125. /// 将对象转时间类型
  126. /// </summary>
  127. /// <param name="str"></param>
  128. /// <returns></returns>
  129. public static DateTime ToDateTime(this string str)
  130. {
  131. DateTime dt;
  132. if (DateTime.TryParse(str, out dt))
  133. {
  134. return dt;
  135. }
  136. else
  137. {
  138. return DateTime.MinValue;//返回最小的时间
  139. }
  140. }
  141. /// <summary>
  142. /// 将对象反射赋值
  143. /// </summary>
  144. /// <typeparam name="S"></typeparam>
  145. /// <typeparam name="T"></typeparam>
  146. /// <param name="s">原对象</param>
  147. /// <param name="t">转换后的对象</param>
  148. /// <returns></returns>
  149. public static T AutoMapping<T>(this object s) where T : new()
  150. {
  151. T t=new T();
  152. ObjectReflection.AutoMapping<T>(s,t);
  153. return t;
  154. }
  155. /// <summary>
  156. /// 将对象集合反射赋值
  157. /// </summary>
  158. /// <typeparam name="S"></typeparam>
  159. /// <typeparam name="T">原集合</typeparam>
  160. /// <param name="s">转换后集合</param>
  161. /// <returns></returns>
  162. public static List<T> AutoMapping<S, T>(this List<S> s) where T : new()
  163. {
  164. List<T> t = new List<T>();
  165. ObjectReflection.AutoMapping<S,T>(s, t);
  166. return t;
  167. }
  168. //获取枚举描述信息
  169. public static string GetDescription(this Enum value)
  170. {
  171. Type type = value.GetType();
  172. string name = Enum.GetName(type, value);
  173. if (!name.IsNull())
  174. {
  175. FieldInfo fieldInfo = type.GetField(name);
  176. if (!fieldInfo.IsNull())
  177. {
  178. DescriptionAttribute attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;
  179. if (!attr.IsNull())
  180. {
  181. return attr.Description;
  182. }
  183. }
  184. }
  185. return null;
  186. }
  187. }
  188. }