Tools.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Reflection;
  6. using System.Text.RegularExpressions;
  7. namespace PMS.Plugins.Common
  8. {
  9. public class Tools
  10. {
  11. public static List<T> ConvertToList<T>(DataTable dt) where T : class, new()
  12. {
  13. // 定义集合
  14. List<T> ts = new List<T>();
  15. // 获得此模型的类型
  16. Type type = typeof(T);
  17. string tempName = "";
  18. foreach (DataRow dr in dt.Rows)
  19. {
  20. T t = new T();
  21. // 获得此模型的公共属性
  22. PropertyInfo[] propertys = t.GetType().GetProperties();
  23. foreach (PropertyInfo pi in propertys)
  24. {
  25. tempName = pi.Name;
  26. // 检查DataTable是否包含此列
  27. if (dt.Columns.Contains(tempName))
  28. {
  29. // 判断此属性是否有Setter
  30. if (!pi.CanWrite) continue;
  31. object value = dr[tempName];
  32. if (value != DBNull.Value)
  33. {
  34. //判断如果目标类型为Nullable类型
  35. if (pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
  36. {
  37. if (value == null)
  38. {
  39. return null;
  40. }
  41. NullableConverter nullableConverter = new NullableConverter(pi.PropertyType);
  42. Type conversionType = nullableConverter.UnderlyingType;
  43. pi.SetValue(t, Convert.ChangeType(value, conversionType), null);
  44. }
  45. else
  46. {
  47. pi.SetValue(t, Convert.ChangeType(value, pi.PropertyType), null);
  48. }
  49. }
  50. else
  51. {
  52. var strValue = value.ToStringEX();
  53. if (strValue.IsNullOrEmpty())
  54. {
  55. if (pi.PropertyType.Name.StartsWith("String", StringComparison.Ordinal))
  56. {
  57. value = "";
  58. pi.SetValue(t, Convert.ChangeType(value, pi.PropertyType), null);
  59. }
  60. else
  61. {
  62. value = 0;
  63. try
  64. {
  65. pi.SetValue(t, Convert.ChangeType(value, pi.PropertyType), null);
  66. }
  67. catch (Exception)
  68. {
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }
  75. ts.Add(t);
  76. }
  77. return ts;
  78. }
  79. public static string RemoveHTML(string strHtml)
  80. {
  81. string[] aryReg ={
  82. @"<script[^>]*?>.*?</script>",
  83. @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
  84. @"([\r\n])[\s]+",
  85. @"&(quot|#34);",
  86. @"&(amp|#38);",
  87. @"&(lt|#60);",
  88. @"&(gt|#62);",
  89. @"&(nbsp|#160);",
  90. @"&(iexcl|#161);",
  91. @"&(cent|#162);",
  92. @"&(pound|#163);",
  93. @"&(copy|#169);",
  94. @"&#(\d+);",
  95. @"-->",
  96. @"<!--.*\n"
  97. };
  98. string[] aryRep = {
  99. "",
  100. "",
  101. "",
  102. "\"",
  103. "&",
  104. "<",
  105. ">",
  106. " ",
  107. "\xa1",//chr(161),
  108. "\xa2",//chr(162),
  109. "\xa3",//chr(163),
  110. "\xa9",//chr(169),
  111. "",
  112. "\r\n",
  113. ""
  114. };
  115. string newReg = aryReg[0];
  116. string strOutput = strHtml;
  117. for (int i = 0; i < aryReg.Length; i++)
  118. {
  119. Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
  120. strOutput = regex.Replace(strOutput, aryRep[i]);
  121. }
  122. strOutput.Replace("<", "");
  123. strOutput.Replace(">", "");
  124. strOutput.Replace("\r\n", "");
  125. return strOutput;
  126. }
  127. }
  128. }