一些工具函数--object 于 DataTable 相互转换

 using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Reflection; using System.Collections; namespace CertificateReport.Utility { ///

/// Represents some utility functions for object-datatable manipulation /// public sealed class OTConverter { /// /// Convert an DataRow to an object /// /// /// /// public static T ConvertToObject(DataRow row) where T:new() { object obj = new T(); if (row != null) { DataTable t = row.Table; GetObject(t.Columns, row, obj); } if (obj != null && obj is T) return (T)obj; else return default(T); } /// /// Convert a data table to an objct list /// /// /// /// public static List ConvertTableToObject(DataTable t) where T:new() { List list = new List(); foreach (DataRow row in t.Rows) { T obj = ConvertToObject(row); list.Add(obj); } return list; } /// /// Convert object collection to an data table /// /// /// public static DataTable GenericListToDataTable(object list) { DataTable dt = null; Type listType = list.GetType(); if (listType.IsGenericType) { //determine the underlying type the List<> contains Type elementType = listType.GetGenericArguments()[0]; //create empty table -- give it a name in case //it needs to be serialized dt = new DataTable(elementType.Name + "List"); //define the table -- add a column for each public //property or field MemberInfo[] miArray = elementType.GetMembers( BindingFlags.Public | BindingFlags.Instance); foreach (MemberInfo mi in miArray) { if (mi.MemberType == MemberTypes.Property) { PropertyInfo pi = mi as PropertyInfo; dt.Columns.Add(pi.Name, pi.PropertyType); } else if (mi.MemberType == MemberTypes.Field) { FieldInfo fi = mi as FieldInfo; dt.Columns.Add(fi.Name, fi.FieldType); } } //populate the table IList il = list as IList; foreach (object record in il) { int i = 0; object[] fieldValues = new object[dt.Columns.Count]; foreach (DataColumn c in dt.Columns) { MemberInfo mi = elementType.GetMember(c.ColumnName)[0]; if (mi.MemberType == MemberTypes.Property) { PropertyInfo pi = mi as PropertyInfo; fieldValues[i] = pi.GetValue(record, null); } else if (mi.MemberType == MemberTypes.Field) { FieldInfo fi = mi as FieldInfo; fieldValues[i] = fi.GetValue(record); } i++; } dt.Rows.Add(fieldValues); } } return dt; } #region "internal methods" private static void GetObject(DataColumnCollection cols, DataRow dr, Object obj) { Type t = obj.GetType(); //This is used to do the reflection PropertyInfo[] props = t.GetProperties(); foreach (PropertyInfo pro in props) { if (cols.Contains(pro.Name)) { pro.SetValue(obj, dr[pro.Name] == DBNull.Value ? null : dr[pro.Name], null); } } //for (Int32 i = 0; i <= cols.Count - 1; i++) //{ // try // { //NOTE the datarow column names must match exactly (including case) to the object property names // t.InvokeMember(cols[i].ColumnName // , BindingFlags.SetProperty // , null // , obj // , new object[] { dr[cols[i].ColumnName] }); // } // catch (Exception ex) // { //Usually you are getting here because a column doesn't exist or it is null // if (ex.ToString() != null) // { // } // } //}; } #endregion } }

你可能感兴趣的:(Web,开发,Microsoft,.NET)