private static void FireAction <T>(DBMapOption option, object dataRow, T contextObject) { if (option != null && option.RowFillAction != null && contextObject != null) { option.RowFillAction(contextObject, dataRow, option.RowFillActionParam); } }
/// <summary> /// 生成实体集合 /// </summary> /// <typeparam name="T">集合元素对象类型</typeparam> /// <param name="dt">数据集合</param> /// <param name="option">映射选项</param> /// <returns>返回实体集合</returns> public static IEnumerable <T> ToList <T>(this DataTable dt, DBMapOption option = null) where T : new() { if (dt != null && dt.Rows.Count > 0) { foreach (DataRow row in dt.Rows) { yield return(row.Fill <T>(option)); } } }
/// <summary> /// 生成实体集合 /// </summary> /// <typeparam name="T">集合元素对象类型</typeparam> /// <param name="rows">数据集合</param> /// <param name="option">映射选项</param> /// <returns>返回实体集合</returns> public static IEnumerable <T> ToList <T>(this DataRowCollection rows, DBMapOption option = null) where T : new() { if (rows != null && rows.Count > 0) { foreach (DataRow row in rows) { yield return(row.Fill <T>(option)); } } }
/// <summary> /// 生成实体集合 /// </summary> /// <typeparam name="T">集合元素对象类型</typeparam> /// <param name="reader">数据集合</param> /// <param name="option">映射选项</param> /// <returns>返回实体集合</returns> public static IEnumerable <T> ToList <T>(this IDataReader reader, DBMapOption option = null) where T : new() { if (reader != null) { while (reader.Read()) { yield return(reader.Fill <T>(option)); } reader.Close(); } }
private static bool IsIgnoreField(string name, DBMapOption option) { if (option == null || option.IgnoreFields == null || option.IgnoreFields.Count() == 0)//不忽略任何字段 { return(false); } foreach (string item in option.IgnoreFields) { if (item.Trim() == name.Trim()) { return(true);//忽略 } } return(false); }
/// <summary> /// 生成实体 /// </summary> /// <typeparam name="T">要填充的实体类型</typeparam> /// <param name="row">数据行</param> /// <param name="option">映射选项</param> /// <returns>返回实体</returns> public static T Fill <T>(this DataRow row, DBMapOption option = null) where T : new() { if (row != null) { PropertyInfo[] ps = typeof(T).GetProperties(); if (ps != null && ps.Length > 0) { T re = new T(); foreach (PropertyInfo p in ps) { if (p != null && p.CanWrite && !IsIgnoreField(p.Name, option)) { Type targetPType = null; if (p.PropertyType.IsGenericType) { targetPType = p.PropertyType.GetGenericArguments()[0]; } else if (typeof(IConvertible).IsAssignableFrom(p.PropertyType)) { targetPType = p.PropertyType; } if (targetPType != null) { p.SetValue(re, TryConvertType(ReadValue(row, MapFieldName(p.Name, (option == null ? null : option.FiledNameMaps))), targetPType, p.PropertyType.IsGenericType)); } } } FireAction <T>(option, row, re); return(re); } } return(default(T)); }