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 ConvertToList(DataTable dt) where T : class, new() { // 定义集合 List ts = new List(); // 获得此模型的类型 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 ={ @"]*?>.*?", @"<(\/\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+);", @"-->", @"