using System; using System.Collections.Generic; using System.Data; using System.Reflection; namespace DeviceRepair.Utils { public static class DataExtend { public static T ToObject(this DataRow row) where T : new() { T item = new T(); foreach (PropertyInfo property in typeof(T).GetProperties()) { if (row.Table.Columns.Contains(property.Name)) { if (DBNull.Value != row[property.Name]) { property.SetValue(item, Convert.ChangeType(row[property.Name], property.PropertyType), null); } } } return item; } public static List ToList(this DataTable dataTable) where T : new() { var dataList = new List(); // 获取目标类型的属性 var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (DataRow row in dataTable.Rows) { var obj = new T(); foreach (var property in properties) { if (dataTable.Columns.Contains(property.Name)) { Type propertyType = property.PropertyType; object value = row[property.Name]; if (value != DBNull.Value) { if (propertyType.BaseType == typeof(Enum)) { property.SetValue(obj, Enum.Parse(propertyType, (value?.ToString() ?? ""))); } else { // 当Property是Nullable类型时,需要获取其UnderlyingType property.SetValue(obj, Convert.ChangeType(value, Nullable.GetUnderlyingType(propertyType) ?? propertyType)); } } } } dataList.Add(obj); } return dataList; } /// /// Convert a List{T} to a DataTable. /// public static DataTable ToDataTable(this List items) { var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in props) { Type t = GetCoreType(prop.PropertyType); tb.Columns.Add(prop.Name, t); } foreach (T item in items) { var values = new object[props.Length]; for (int i = 0; i < props.Length; i++) { values[i] = props[i].GetValue(item, null); } tb.Rows.Add(values); } return tb; } public static DataTable toDataTable(this T item) { var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo prop in props) { Type t = GetCoreType(prop.PropertyType); tb.Columns.Add(prop.Name, t); } var values = new object[props.Length]; for (int i = 0; i < props.Length; i++) { values[i] = props[i].GetValue(item, null); } tb.Rows.Add(values); return tb; } /// /// Determine of specified type is nullable 指定类型的判定为空 /// public static bool IsNullable(Type t) { return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)); } /// /// Return underlying type if type is Nullable otherwise return the type /// 如果类型为空则返回基础类型,否则返回类型 /// public static Type GetCoreType(Type t) { if (t != null && IsNullable(t)) { if (!t.IsValueType) { return t; } else { return Nullable.GetUnderlyingType(t); } } else { return t; } } /// /// 解压数据返回DataSet /// /// /// public static DataSet ExactDataSet(this byte[] addDs) { if (addDs == null || addDs.Length == 0) { return new DataSet(); } byte[] kao = ExactZip(addDs); System.Data.DataSet ds = new System.Data.DataSet(); System.IO.MemoryStream ms = new System.IO.MemoryStream(); ms.Write(kao, 0, kao.Length); ms.Seek(0, System.IO.SeekOrigin.Begin); System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(ms); ds.ReadXml(reader, System.Data.XmlReadMode.Auto); ms.Close(); return ds; } public static byte[] ExactZip(byte[] needExact) { System.IO.MemoryStream ret = new System.IO.MemoryStream(needExact); System.IO.MemoryStream ret1 = new System.IO.MemoryStream(); ICSharpCode.SharpZipLib.GZip.GZipInputStream zipIn = new ICSharpCode.SharpZipLib.GZip.GZipInputStream(ret); byte[] buffer = new byte[2048]; int size = 2048; while (true) { size = zipIn.Read(buffer, 0, buffer.Length); if (size > 0) { ret1.Write(buffer, 0, size); } else { break; } } return ret1.ToArray(); } public static void ForEach(this IEnumerable source, Action action) { foreach (var item in source) { action(item); } } } }