C#打造自己的实体生成器(12页).doc
《C#打造自己的实体生成器(12页).doc》由会员分享,可在线阅读,更多相关《C#打造自己的实体生成器(12页).doc(12页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、-C#打造自己的实体生成器-第 12 页 using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Reflection; / / 实体阅读器类,可以从DataTable中或者DbDataReader的实例中将数据转换成对应的示例 / 说明:(1)任何人都可以免费使用,请尽量保持此段说明。 / (2)这个版本还不是最终版本,有任何意见或建议请到 / public sealed class EntityReader private const
2、BindingFlags BindingFlag = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; /将类型与该类型所有的可写且未被忽略属性之间建立映射 private static DictionaryType, Dictionary propertyMappings = new DictionaryType, Dictionary(); /存储Nullable与T的对应关系 private static Dictionary genericTypeMappings = new Dictionary
3、(); static EntityReader() genericTypeMappings.Add(typeof(Byte?), typeof(Byte); genericTypeMappings.Add(typeof(SByte?), typeof(SByte); genericTypeMappings.Add(typeof(Char?), typeof(Char); genericTypeMappings.Add(typeof(Boolean?), typeof(Boolean); genericTypeMappings.Add(typeof(Guid?), typeof(Guid); g
4、enericTypeMappings.Add(typeof(Int16), typeof(Int16); genericTypeMappings.Add(typeof(UInt16), typeof(UInt16); genericTypeMappings.Add(typeof(Int32), typeof(Int32); genericTypeMappings.Add(typeof(UInt32), typeof(UInt32); genericTypeMappings.Add(typeof(Int64), typeof(Int64); genericTypeMappings.Add(typ
5、eof(UInt64), typeof(UInt64); genericTypeMappings.Add(typeof(Single), typeof(Single); genericTypeMappings.Add(typeof(Double), typeof(Double); genericTypeMappings.Add(typeof(Decimal), typeof(Decimal); genericTypeMappings.Add(typeof(DateTime), typeof(DateTime); genericTypeMappings.Add(typeof(TimeSpan),
6、 typeof(TimeSpan); genericTypeMappings.Add(typeof(Enum), typeof(Enum); / / 将DataTable中的所有数据转换成List>T<集合 / / DataTable中每条数据可以转换的数据类型 / 包含有可以转换成数据类型T的数据集合 / public static List GetEntities(DataTable dataTable) where T : new() if (dataTable = null) throw new ArgumentNullException(dataTable); /如果T的
7、类型满足以下条件:字符串、ValueType或者是Nullable if(typeof(T)=typeof(string)|typeof(T).IsValueType) return GetSimpleEntities(dataTable); else return GetComplexEntities(dataTable); / / 将DbDataReader中的所有数据转换成List>T<集合 / / DbDataReader中每条数据可以转换的数据类型 / 包含有可以转换成数据类型T的DbDataReader实例 / public static List GetEntitie
8、s(DbDataReader reader) where T : new() List list = new List(); if (reader = null) throw new ArgumentNullException(reader); /如果T的类型满足以下条件:字符串、ValueType或者是Nullable if (typeof(T) = typeof(string) | typeof(T).IsValueType) return GetSimpleEntities(reader); else return GetComplexEntities(reader); / / 从Dat
9、aTable中将每一行的第一列转换成T类型的数据 / / 要转换的目标数据类型 / 包含有可以转换成数据类型T的数据集合 / private static List GetSimpleEntities(DataTable dataTable) where T : new() List list = new List(); foreach (DataRow row in dataTable.Rows) list.Add(T)GetValueFromObject(row0, typeof(T); return list; / / 将指定的 Object 的值转换为指定类型的值。 / / 实现 IC
10、onvertible 接口的 Object,或者为 null / 要转换的目标数据类型 / private static object GetValueFromObject(object value, Type targetType) if (targetType = typeof(string)/如果要将value转换成string类型 return GetString(value); else if (targetType.IsGenericType)/如果目标类型是泛型 return GetGenericValueFromObject(value, targetType); else/如
11、果是基本数据类型(包括数值类型、枚举和Guid) return GetNonGenericValueFromObject(value, targetType); / / 从DataTable中读取复杂数据类型集合 / / 要转换的目标数据类型 / 包含有可以转换成数据类型T的数据集合 / private static List GetComplexEntities(DataTable dataTable) where T : new() if (!propertyMappings.ContainsKey(typeof(T) GenerateTypePropertyMapping(typeof(
12、T); List list = new List(); Dictionary properties = propertyMappingstypeof(T); /Dictionary propertyColumnOrdinalMapping = GetPropertyColumnIndexMapping(dataTable.Columns, properties); T t; foreach (DataRow row in dataTable.Rows) t = new T(); foreach (KeyValuePair item in properties) /int ordinal = -
13、1; /if (propertyColumnOrdinalMapping.TryGetValue(item.Key, out ordinal) / item.Value.SetValue(t, GetValueFromObject(rowordinal, item.Value.PropertyType), null); item.Value.SetValue(t, GetValueFromObject(rowitem.Key, item.Value.PropertyType), null); list.Add(t); return list; / / 从DbDataReader的实例中读取复杂
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 打造 自己 实体 生成器 12
限制150内