using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.Data; namespace Estsh.Web.Util { public static class ModelConvert { /// /// 根据模板获得该类可读写的字段名数组 /// /// 模版类 /// 字段名数组 public static string[] GetFieldNames() { List fieldList = new List(); Type type = typeof(T); PropertyInfo[] propertys = type.GetProperties(); for (int i = 0; i < propertys.Length; i++) { // 只包含可读写的字段 if (propertys[i].CanRead && propertys[i].CanWrite) { fieldList.Add(propertys[i].Name); } } return fieldList.ToArray(); } /// /// 根据模板类获得实体的字段键值对 /// /// 模版类 /// 实体 /// 字段键值对 public static Dictionary GetFieldNameValuePairs(T model) { Dictionary valueDict = new Dictionary(); Type type = typeof(T); PropertyInfo[] propertys = type.GetProperties(); for (int i = 0; i < propertys.Length; i++) { // 只包含可读写的字段 if (propertys[i].CanRead && propertys[i].CanWrite) { object value = propertys[i].GetValue(model, null); valueDict.Add(propertys[i].Name, value); } } return valueDict; } /// /// 根据实体的属性构造一张空表 /// /// 模版类 /// public static DataTable BuildSchema() { DataTable tbSchema = new DataTable(); Type type = typeof(T); foreach (PropertyInfo property in type.GetProperties()) { tbSchema.Columns.Add(property.Name, property.PropertyType); } return tbSchema; } /// /// 实体数组到数据表的赋值 /// /// 模版类 /// /// public static DataTable ToDataTable(T[] modelArray) { DataTable dt = BuildSchema(); for (int i = 0; i < modelArray.Length; i++) { DataRow row = ToDataRow(modelArray[i], dt); dt.Rows.Add(row); } return dt; } /// /// 实体到数据行的同名字段同步 /// /// 模版类 /// /// /// public static DataRow ToDataRow(T model, DataTable dt) { DataRow row = dt.NewRow(); Type type = typeof(T); foreach (PropertyInfo property in type.GetProperties()) { if (row.Table.Columns.Contains(property.Name)) { row[property.Name] = property.GetValue(model, null); } } return row; } /// /// 数据表到实体数组的赋值 /// /// 模版类 /// /// public static T[] ToModelArray(DataTable dt) { T[] modelArray = new T[dt.Rows.Count]; for (int i = 0; i < dt.Rows.Count; i++) { modelArray[i] = ToModel(dt.Rows[i]); } return modelArray; } /// /// 数据行到实体的同名字段同步 /// /// 模版类 /// /// public static T ToModel(DataRow row) { Type type = typeof(T); T model = (T)Activator.CreateInstance(type); foreach (PropertyInfo property in type.GetProperties()) { if (row.Table.Columns.Contains(property.Name) && property.CanWrite && row[property.Name] != null && row[property.Name] != DBNull.Value) { object value = null; switch (Type.GetTypeCode(property.PropertyType)) { case TypeCode.Boolean: value = Convert.ToBoolean(row[property.Name]); break; case TypeCode.DateTime: value = Convert.ToDateTime(row[property.Name]); break; case TypeCode.Decimal: value = Convert.ToDecimal(row[property.Name]); break; case TypeCode.Single: value = Convert.ToSingle(row[property.Name]); break; case TypeCode.Double: value = Convert.ToDouble(row[property.Name]); break; case TypeCode.Int16: value = Convert.ToInt16(row[property.Name]); break; case TypeCode.Int32: value = Convert.ToInt32(row[property.Name]); break; case TypeCode.Int64: value = Convert.ToInt64(row[property.Name]); break; case TypeCode.SByte: value = Convert.ToSByte(row[property.Name]); break; case TypeCode.Byte: value = Convert.ToByte(row[property.Name]); break; case TypeCode.String: value = Convert.ToString(row[property.Name]); break; default: value = row[property.Name]; break; } property.SetValue(model, value, null); } } return model; } } }