当前位置:Gxlcms > 数据库问题 > 将数据库读出来的数据转化成集合

将数据库读出来的数据转化成集合

时间:2021-07-01 10:21:17 帮助过:22人阅读

class DynamicBuilder<T> { private PropertyInfo[] properties; private DynamicBuilder() { } public T Build(IDataRecord reader) { T result = (T)Activator.CreateInstance(typeof(T)); for (int i = 0; i < reader.FieldCount; i++) { if (properties[i] != null && !reader.IsDBNull(i)) { if (properties[i].PropertyType.IsEnum) { properties[i].SetValue(result, Enum.ToObject(properties[i].PropertyType, reader[i]), null); } else { properties[i].SetValue(result, Convert.ChangeType(reader[i], properties[i].PropertyType), null); } } } return result; } public static DynamicBuilder<T> CreateBuilder(IDataRecord reader) { DynamicBuilder<T> result = new DynamicBuilder<T>(); result.properties = new PropertyInfo[reader.FieldCount]; for (int i = 0; i < reader.FieldCount; i++) { result.properties[i] = typeof(T).GetProperty(reader.GetName(i), BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); } return result; } }

 

将数据库读出来的数据转化成集合

标签:data   mic   cti   bnu   eof   new   name   info   tor   

人气教程排行