public static void AssignedMap <T>(T obj, DataRow row, ReturnMap <T> rMap, bool propertyNameIgnoreCase, bool skipNotExistProperty) where T : class { if (rMap == null) { return; } List <PropertyColumn> maps = rMap.GetMaps(); if (maps == null || maps.Count <= 0) { return; } foreach (PropertyColumn map in maps) { object val = row[map.Column]; if (map.PropertyList == null || map.PropertyList.Count <= 0) { continue; } else { string[] pNames = map.PropertyList.ToArray(); val = DataMapperHelper.ConvertIfEnum(val, typeof(T), pNames, propertyNameIgnoreCase, skipNotExistProperty); FillFieldValue(obj, pNames, val, propertyNameIgnoreCase, skipNotExistProperty); } } }
/// <summary> /// 根据DataRow的数据来填充实体属性 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="obj">待填充的实体对象实例</param> /// <param name="row">读取数据库获得的数据读取器</param> /// <param name="propertyNameIgnoreCase">根据所获取的DataRow数据集中各个列的名字去寻找实体类型中同名属性时,是否区分大小写,true则不区分大小写,默认为true。</param> /// <param name="skipNotExistProperty">当DataRow数据集中某个列的名字在实体类型中找不到对应的同名属性时,是否自动跳过,true则自动跳过,否则抛出异常,默认为true。</param> /// <param name="splitter">SQL结果集中某个列的名字上区分对象属性级别的分隔符,默认为'.'</param> public static void AutoMap <T>(T obj, DataRow row, bool propertyNameIgnoreCase, bool skipNotExistProperty, char splitter, Action <DataRow, T> manualMapper = null) where T : class { for (int i = 0; i < row.Table.Columns.Count; i++) { string[] pNames = row.Table.Columns[i].ColumnName.Split(new char[] { splitter }, StringSplitOptions.RemoveEmptyEntries); object val = DataMapperHelper.ConvertIfEnum(row[i], typeof(T), pNames, propertyNameIgnoreCase, skipNotExistProperty); FillFieldValue(obj, pNames, val, propertyNameIgnoreCase, skipNotExistProperty); } if (manualMapper != null) { manualMapper(row, obj); } }
/// <summary> /// 检查入参type的属性pNames是否为枚举,如果是,则把入参value按照枚举的mapping关系转换为对应的枚举值再返回,否则就直接返回value /// </summary> public static object ConvertIfEnum(object value, Type type, string[] pNames, bool propertyNameIgnoreCase, bool skipNotExistProperty) { if (value == null || value == DBNull.Value) { return(null); } Type pType = DataMapperHelper.GetPropertyType(type, pNames, propertyNameIgnoreCase, skipNotExistProperty); if (pType == null) { return(value); } return(ConvertIfEnum(value, pType)); }
/// <summary> /// 使用这个方法,需要在方法外先调用IDataReader reader的Read()方法后再将reader传入方法 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="obj">待填充的实体对象实例</param> /// <param name="reader">读取数据库获得的数据读取器</param> /// <param name="propertyNameIgnoreCase">根据所获取的SQL结果集中各个列的名字去寻找实体类型中同名属性时,是否区分大小写,true则不区分大小写,默认为true。</param> /// <param name="skipNotExistProperty">当SQL结果集中某个列的名字在实体类型中找不到对应的同名属性时,是否自动跳过,true则自动跳过,否则抛出异常,默认为true。</param> /// <param name="splitter">SQL结果集中某个列的名字上区分对象属性级别的分隔符,默认为'.'</param> public static void NoReadAutoMap <T>(T obj, IDataReader reader, bool propertyNameIgnoreCase, bool skipNotExistProperty, char splitter, Action <IDataReader, T> manualMapper = null) where T : class { for (int i = 0; i < reader.FieldCount; i++) { string[] pNames = reader.GetName(i).Split(new char[] { splitter }, StringSplitOptions.RemoveEmptyEntries); object val = DataMapperHelper.ConvertIfEnum(reader[i], typeof(T), pNames, propertyNameIgnoreCase, skipNotExistProperty); FillFieldValue(obj, pNames, val, propertyNameIgnoreCase, skipNotExistProperty); } if (manualMapper != null) { manualMapper(reader, obj); } }