Extends.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Script.Serialization;
  6. namespace SCC.Crawler.DT
  7. {
  8. public static class Extends
  9. {
  10. /// <summary>
  11. /// 将string类型转换成int类型
  12. /// </summary>
  13. /// <param name="str"></param>
  14. /// <returns></returns>
  15. public static int ToInt(this string str)
  16. {
  17. int ret_number = -1;
  18. int.TryParse(str, out ret_number);
  19. return ret_number;
  20. }
  21. /// <summary>
  22. /// 将以splitChar分割的字符串转换为整形IList数组
  23. /// 注:如果字符串内包含不能转换为整形数据的字符,则返回NULL
  24. /// </summary>
  25. /// <param name="source">源字符串</param>
  26. /// <param name="splitChar">分隔符char</param>
  27. /// <returns></returns>
  28. public static IList<int> ToIntArray(this string source, char splitChar)
  29. {
  30. if (string.IsNullOrEmpty(source))
  31. return null;
  32. string[] data = source.Split(splitChar);
  33. int n = 0;
  34. IList<int> list = new List<int>();
  35. foreach (var item in data)
  36. {
  37. if (Int32.TryParse(item, out n))
  38. { list.Add(n); }
  39. else { return null; }
  40. }
  41. return list;
  42. }
  43. /// <summary>
  44. /// 将string[]类型转换成string(用|分隔)
  45. /// </summary>
  46. /// <param name="str"></param>
  47. /// <returns></returns>
  48. public static string ArrayToString(this string[] str)
  49. {
  50. if (str == null)
  51. return "";
  52. StringBuilder sb = new StringBuilder(str.Length * 50);
  53. foreach (var item in str)
  54. {
  55. sb.Append(item + "|");
  56. }
  57. return sb.ToString().TrimEnd('|');
  58. }
  59. /// <summary>
  60. /// 将string[]类型转换成string(以 第二个参数 分隔)
  61. /// </summary>
  62. /// <param name="str">数组</param>
  63. /// <param name="con">分隔符号</param>
  64. /// <returns></returns>
  65. public static string ArrayToString(this string[] str, char con)
  66. {
  67. if (str == null)
  68. return "";
  69. StringBuilder sb = new StringBuilder(str.Length * 50);
  70. foreach (var item in str)
  71. {
  72. sb.Append(item + con.ToString());
  73. }
  74. return sb.ToString().TrimEnd(con);
  75. }
  76. /// <summary>
  77. /// 将对象转换成JSON
  78. /// </summary>
  79. /// <param name="obj"></param>
  80. /// <returns></returns>
  81. public static string TO_Josin(this object obj)
  82. {
  83. JavaScriptSerializer js = new JavaScriptSerializer();
  84. try
  85. {
  86. return js.Serialize(obj);
  87. }
  88. catch //(Exception ex)
  89. {
  90. return "";
  91. }
  92. }
  93. /// <summary>
  94. /// 将JSON类型转换成对象
  95. /// </summary>
  96. /// <param name="obj"></param>
  97. /// <returns></returns>
  98. public static T TO_Object<T>(this string str)
  99. {
  100. System.Web.Script.Serialization.JavaScriptSerializer js =
  101. new System.Web.Script.Serialization.JavaScriptSerializer();
  102. try
  103. {
  104. return js.Deserialize<T>(str);
  105. }
  106. catch //(Exception ex)
  107. {
  108. return default(T);
  109. }
  110. }
  111. /// <summary>
  112. /// 将Json类型转换成 List对象
  113. /// </summary>
  114. /// <typeparam name="T"></typeparam>
  115. /// <param name="JsonStr"></param>
  116. /// <returns></returns>
  117. public static List<T> JSONStringToList<T>(this string JsonStr)
  118. {
  119. JavaScriptSerializer Serializer = new JavaScriptSerializer();
  120. List<T> objs = Serializer.Deserialize<List<T>>(JsonStr);
  121. return objs;
  122. }
  123. }
  124. }