/// <summary> /// emit转换实体 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dr"></param> /// <returns></returns> public static List <T> ToEntityList <T>(this IDataReader dr) { List <T> list = new List <T>(); LoadDataRecord <T> load = null; if (IsCache) { load = FindDataRecordMethod <T>(dr); } if (load == null) { var properties = typeof(T).GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); var mapColumns = CheckProperty(dr, properties); if (IsCache) { load = CreateDataRecordMethod <T>(dr, mapColumns); } else { load = CreateDataRecordMethod <T>(null, mapColumns); } } while (dr.Read()) { list.Add(load(dr)); } return(list); }
/// <summary> /// 转行实体 /// </summary> /// <param name="dr">DataReader</param> /// <param name="type">实体类型</param> /// <param name="key">缓存Key</param> /// <param name="ignore">忽略属性大小写,默认忽略</param> /// <returns></returns> public static List <object> ToEntityList(this IDataReader dr, Type type, string key = null, bool ignore = true) { List <object> list = new List <object>(); LoadDataRecord <object> load = null; if (IsCache) { load = FindObjectDataRecordMethod(dr, type, key); } if (load == null) { var properties = type.GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); var mapColumns = CheckProperty(dr, properties, ignore); if (IsCache) { load = CreateDataRecordMethod(dr, mapColumns, type, key); } else { load = CreateDataRecordMethod(null, mapColumns, type, key); } } while (dr.Read()) { list.Add(load(dr)); } return(list); }
/// <summary> /// 从Dictionary获取委托 LoadDataRecord<T>的方法实例,没有则调用BuildMethod构造一个。 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> private static LoadDataRecord <T> GetDataRecordMethod <T>() { string key = dataRecordAssembly.MethodName + typeof(T).Name; LoadDataRecord <T> load = null; load = (LoadDataRecord <T>)BuildMethod <T>(dataRecordAssembly).CreateDelegate(typeof(LoadDataRecord <T>)); return(load); }
public static List <T> ToList <T>(IDataReader dr) { List <T> list = new List <T>(); LoadDataRecord <T> load = GetDataRecordMethod <T>(); while (dr.Read()) { list.Add(load(dr)); } return(list); }
private static LoadDataRecord <object> CreateDataRecordMethod(IDataReader reader, MapColumn[] mapColumns, Type type, string key = null) { if (reader != null && string.IsNullOrEmpty(key)) { key = reader.FieldCount + "_" + dataRecordAssembly.MethodName + type.FullName; } LoadDataRecord <object> load = (LoadDataRecord <object>)BuildMethod(dataRecordAssembly, type, mapColumns, key).CreateDelegate(typeof(LoadDataRecord <object>)); if (key != null) { ConvertCache <string, object> .Singleton.Set(key, load); } return(load); }
/// <summary> /// 查找DataRecord转换 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="reader"></param> /// <returns></returns> private static LoadDataRecord <T> FindDataRecordMethod <T>(IDataReader reader, string key = null) { if (string.IsNullOrEmpty(key)) { key = reader.FieldCount + "_" + dataRecordAssembly.MethodName + typeof(T).FullName; } LoadDataRecord <T> load = null; object v = null; if (ConvertCache <string, object> .Singleton.TryGet(key, out v)) { load = v as LoadDataRecord <T>; } return(load); }
/// <summary> /// 从Dictionary获取委托 LoadDataRecord<T>的方法实例,没有则调用BuildMethod构造一个。 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> private static LoadDataRecord <T> GetDataRecordMethod <T>() { string key = dataRecordAssembly.MethodName + typeof(T).Name; LoadDataRecord <T> load = null; if (HttpRuntime.Cache[key] == null) { load = (LoadDataRecord <T>)BuildMethod <T>(dataRecordAssembly).CreateDelegate(typeof(LoadDataRecord <T>)); HttpRuntime.Cache[key] = load; } else { load = HttpRuntime.Cache[key] as LoadDataRecord <T>; } return(load); }
/// <summary> /// 查找DataRecord转换 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="reader"></param> /// <returns></returns> private static LoadDataRecord <T> FindDataRecordMethod <T>(IDataReader reader) { string key = reader.FieldCount + "_"; for (int i = 0; i < reader.FieldCount; i++) { key = key + reader.GetName(i) + "_"; } key = key + dataRecordAssembly.MethodName + typeof(T).FullName; LoadDataRecord <T> load = null; object v = null; if (ConvertCache <string, object> .Singleton.TryGet(key, out v)) { load = v as LoadDataRecord <T>; } return(load); }
/// <summary> /// 从Cache获取委托 LoadDataRecord<T>的方法实例,没有则调用BuildMethod构造一个。 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> private static LoadDataRecord <T> GetDataRecordMethod <T>() { string key = dataRecordAssembly.MethodName + typeof(T).Name; LoadDataRecord <T> load = null; if (Cache == null) { Cache = new Dictionary <string, object>(); } if (!Cache.ContainsKey(key)) { load = (LoadDataRecord <T>)BuildMethod <T>(dataRecordAssembly).CreateDelegate(typeof(LoadDataRecord <T>)); Cache.Add(key, load); } else { load = Cache[key] as LoadDataRecord <T>; } return(load); }
/// <summary> /// 创建DataRecord转换 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="reader"></param> /// <param name="mapColumns"></param> /// <returns></returns> private static LoadDataRecord <T> CreateDataRecordMethod <T>(IDataReader reader, MapColumn[] mapColumns) { string key = null; if (reader != null) { key = reader.FieldCount + "_"; for (int i = 0; i < reader.FieldCount; i++) { key = key + reader.GetName(i) + "_"; } key = key + dataRecordAssembly.MethodName + typeof(T).FullName; } LoadDataRecord <T> load = (LoadDataRecord <T>)BuildMethod <T>(dataRecordAssembly, mapColumns, key).CreateDelegate(typeof(LoadDataRecord <T>)); if (key != null) { ConvertCache <string, object> .Singleton.Set(key, load); } return(load); }