123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Reflection;
- using System.Text.RegularExpressions;
- namespace PMS.Plugins.Common
- {
- public class Tools
- {
- public static List<T> ConvertToList<T>(DataTable dt) where T : class, new()
- {
- // 定义集合
- List<T> ts = new List<T>();
- // 获得此模型的类型
- Type type = typeof(T);
- string tempName = "";
- foreach (DataRow dr in dt.Rows)
- {
- T t = new T();
- // 获得此模型的公共属性
- PropertyInfo[] propertys = t.GetType().GetProperties();
- foreach (PropertyInfo pi in propertys)
- {
- tempName = pi.Name;
- // 检查DataTable是否包含此列
- if (dt.Columns.Contains(tempName))
- {
- // 判断此属性是否有Setter
- if (!pi.CanWrite) continue;
- object value = dr[tempName];
- if (value != DBNull.Value)
- {
- //判断如果目标类型为Nullable类型
- if (pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
- {
- if (value == null)
- {
- return null;
- }
- NullableConverter nullableConverter = new NullableConverter(pi.PropertyType);
- Type conversionType = nullableConverter.UnderlyingType;
- pi.SetValue(t, Convert.ChangeType(value, conversionType), null);
- }
- else
- {
- pi.SetValue(t, Convert.ChangeType(value, pi.PropertyType), null);
- }
- }
- else
- {
- var strValue = value.ToStringEX();
- if (strValue.IsNullOrEmpty())
- {
- if (pi.PropertyType.Name.StartsWith("String", StringComparison.Ordinal))
- {
- value = "";
- pi.SetValue(t, Convert.ChangeType(value, pi.PropertyType), null);
- }
- else
- {
- value = 0;
- try
- {
- pi.SetValue(t, Convert.ChangeType(value, pi.PropertyType), null);
- }
- catch (Exception)
- {
- }
- }
- }
- }
- }
- }
- ts.Add(t);
- }
- return ts;
- }
- public static string RemoveHTML(string strHtml)
- {
- string[] aryReg ={
- @"<script[^>]*?>.*?</script>",
- @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
- @"([\r\n])[\s]+",
- @"&(quot|#34);",
- @"&(amp|#38);",
- @"&(lt|#60);",
- @"&(gt|#62);",
- @"&(nbsp|#160);",
- @"&(iexcl|#161);",
- @"&(cent|#162);",
- @"&(pound|#163);",
- @"&(copy|#169);",
- @"&#(\d+);",
- @"-->",
- @"<!--.*\n"
- };
- string[] aryRep = {
- "",
- "",
- "",
- "\"",
- "&",
- "<",
- ">",
- " ",
- "\xa1",//chr(161),
- "\xa2",//chr(162),
- "\xa3",//chr(163),
- "\xa9",//chr(169),
- "",
- "\r\n",
- ""
- };
- string newReg = aryReg[0];
- string strOutput = strHtml;
- for (int i = 0; i < aryReg.Length; i++)
- {
- Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
- strOutput = regex.Replace(strOutput, aryRep[i]);
- }
- strOutput.Replace("<", "");
- strOutput.Replace(">", "");
- strOutput.Replace("\r\n", "");
- return strOutput;
- }
- }
- }
|