public Encoding Encoding; // encoding convert from raw public void SetValue(DataRow row, ref object item, ResultListMode mode) { var value = GetValue(row); var isValued = PropertyInfo.GetValue(item, null) != null; if (isValued && mode == ResultListMode.Value) { return; } PropertyInfo.SetValue(item, value, null); }
public static object RowToObject(DataRow row, Type instanceType, object instance, Func <DataRow, object> objectFactory = null, ResultListMode mode = ResultListMode.All, Encoding hardwordEncoding = null) { if (IsAnonymousType(instanceType)) { var wrappers = GetWrappers(GetColumnNames(row.Table.Columns), instanceType, true, hardwordEncoding); return(InternalRowToObjectByConstructor(row, instanceType, wrappers)); } else { var wrappers = GetWrappers(GetColumnNames(row.Table.Columns), instanceType, false, hardwordEncoding); if (instance != null) { return(InternalRowToObjectBySetter(row, instance, wrappers, mode)); } if (objectFactory == null) { objectFactory = r => Activator.CreateInstance(instanceType); } instance = objectFactory(row); return(InternalRowToObjectBySetter(row, instance, wrappers, mode)); } }
internal static object InternalRowToObjectBySetter(DataRow row, object instance, IEnumerable <RowPropertyWrapper> wrappers, ResultListMode mode) { foreach (var wrapper in wrappers) { wrapper.SetValue(row, ref instance, mode); } return(instance); }
public static object ResultFirst(IAncestorResult result, Type[] dataTypes, Delegate objectFactory, ResultListMode mode, Encoding hardwordEncoding) { var list = InternalResultList(result, dataTypes, objectFactory, true, mode, hardwordEncoding); return(list.Count == 0 ? null : list[0]); }
public static IEnumerable <T> TableToCollection <T>(DataTable table, Func <DataRow, object> objectFactory = null, bool firstOnly = false, ResultListMode mode = ResultListMode.All, Encoding hardwordEncoding = null) { return((IEnumerable <T>)TableToCollection(table, typeof(T), objectFactory, firstOnly, mode, hardwordEncoding)); }
public static IEnumerable TableToCollection(DataTable table, Type instanceType, Func <DataRow, object> objectFactory, bool firstOnly, ResultListMode mode, Encoding hardwordEncoding) { if (IsAnonymousType(instanceType)) { var wrappers = GetWrappers(GetColumnNames(table.Columns), instanceType, true, hardwordEncoding); foreach (DataRow row in table.Rows) { yield return(InternalRowToObjectByConstructor(row, instanceType, wrappers)); if (firstOnly) { yield break; } } } else { var wrappers = GetWrappers(GetColumnNames(table.Columns), instanceType, false, hardwordEncoding); if (objectFactory == null) { objectFactory = r => Activator.CreateInstance(instanceType); } foreach (DataRow row in table.Rows) { var instance = objectFactory(row); yield return(InternalRowToObjectBySetter(row, instance, wrappers, mode)); if (firstOnly) { yield break; } } } }
private static object DeepCloneItem(object src, object dst, ref Dictionary <PropertyInfo, Tuple <PropertyInfo, Func <object, object> > > propertyMap, IDictionary <PropertyInfo, HardWordAttribute> hardWords, ResultListMode mode, Encoding hardwordEncoding) { if (propertyMap == null) { var srcProps = src.GetType().GetProperties(); propertyMap = new Dictionary <PropertyInfo, Tuple <PropertyInfo, Func <object, object> > >(); var dstType = dst.GetType(); foreach (var srcProp in srcProps) { var dstProp = dstType.GetProperty(srcProp.Name); if (dstProp != null) { Func <object, object> converter = null; HardWordAttribute attr; if (hardWords.TryGetValue(dstProp, out attr)) { converter = o => { var hex = o as string; var encoding = hardwordEncoding ?? attr.Encoding; return(GetValueFormHex(hex, encoding)); }; } else { converter = v => v; } propertyMap.Add(srcProp, Tuple.Create(dstProp, converter)); } } } foreach (var srcProp in propertyMap.Keys) { var tuple = propertyMap[srcProp]; var value = srcProp.GetValue(src, null); var isValued = tuple.Item1.GetValue(dst, null) != null; if (isValued && mode == ResultListMode.Value) { continue; } if (srcProp.PropertyType == tuple.Item1.PropertyType) { value = tuple.Item2(value); tuple.Item1.SetValue(dst, value, null); } else { value = Convert.ChangeType(value, tuple.Item1.PropertyType); value = tuple.Item2(value); tuple.Item1.SetValue(dst, value, null); } } return(dst); }
internal static IList InternalResultList(IAncestorResult result, Type[] dataTypes, Delegate objectFactory, bool firstOnly, ResultListMode mode, Encoding hardwordEncoding) { var baseTupleType = Type.GetType("System.Tuple`" + dataTypes.Length); var tupleType = baseTupleType.MakeGenericType(dataTypes); IList list = Activator.CreateInstance(typeof(List <>).MakeGenericType(tupleType)) as IList; switch (result.DataType) { case AncestorResultDataType.List: if (result.DataList.Count > 0) // if has item than find anonymouse creation info { var hdMap = new Dictionary <Type, IDictionary <PropertyInfo, HardWordAttribute> >(); foreach (var dataType in dataTypes) { var hds = HardWordManager.Get(dataType); hdMap.Add(dataType, hds); } var map = new Dictionary <Type, Dictionary <PropertyInfo, Tuple <PropertyInfo, Func <object, object> > > >(); var itemList = CastToItem(result.DataList, o => { var args = new object[dataTypes.Length]; for (var i = 0; i < dataTypes.Length; i++) { Dictionary <PropertyInfo, Tuple <PropertyInfo, Func <object, object> > > propertyMap; map.TryGetValue(dataTypes[i], out propertyMap); var flgPropertyMapEmpty = propertyMap == null; IDictionary <PropertyInfo, HardWordAttribute> hds; hdMap.TryGetValue(dataTypes[i], out hds); var ins = Activator.CreateInstance(dataTypes[i]); DeepCloneItem(o, ins, ref propertyMap, hds, mode, hardwordEncoding); if (flgPropertyMapEmpty) { map.Add(dataTypes[i], propertyMap); } args[i] = ins; } return(Activator.CreateInstance(tupleType, args)); }); foreach (var item in itemList) { list.Add(item); if (firstOnly) { break; } } } break; case AncestorResultDataType.DataTable: var rowObjectFactory = objectFactory as Func <DataRow, object>; var listArray = new IEnumerable[dataTypes.Length]; for (var i = 0; i < dataTypes.Length; i++) { listArray[i] = TableToCollection(result.ReturnDataTable, dataTypes[i], rowObjectFactory, firstOnly, mode, hardwordEncoding); } var enumerators = listArray.Select(r => r.GetEnumerator()).ToArray(); while (enumerators.All(r => r.MoveNext())) { var listItem = Activator.CreateInstance(tupleType, enumerators.Select(r => r.Current).ToArray()); list.Add(listItem); } break; } return(list); }
internal static IList InternalResultList(IAncestorResult result, Type dataType, Delegate objectFactory, bool firstOnly, ResultListMode mode, Encoding hardwordEncoding) { IList list = Activator.CreateInstance(typeof(List <>).MakeGenericType(dataType)) as IList; var hds = HardWordManager.Get(dataType); var factory = objectFactory as Func <object>; switch (result.DataType) { case AncestorResultDataType.List: if (InternalHelper.IsAnonymousType(dataType) && factory != null) { if (result.DataList.Count > 0) // if has item than find anonymouse creation info { var targetProperties = dataType.GetProperties(); var sourceProperties = result.DataList[0].GetType().GetProperties(); var resolvers = new List <Func <object, object> >(); foreach (var targetProperty in targetProperties) { var property = sourceProperties.FirstOrDefault(p => p.Name == targetProperty.Name && p.PropertyType == targetProperty.PropertyType); if (property != null) { resolvers.Add(o => property.GetValue(o, null)); } else if (targetProperty.PropertyType.IsValueType) { resolvers.Add(o => Activator.CreateInstance(targetProperty.PropertyType)); } else { resolvers.Add(o => null); } } foreach (var item in CloneByCunstructure(result.DataList, dataType, resolvers)) { list.Add(item); if (firstOnly) { break; } } } } else { Dictionary <PropertyInfo, Tuple <PropertyInfo, Func <object, object> > > propertyMap = null; if (factory == null) { factory = () => Activator.CreateInstance(dataType); } foreach (var item in CastToItem(result.DataList, o => DeepCloneItem(o, factory(), ref propertyMap, hds, mode, hardwordEncoding))) { list.Add(item); if (firstOnly) { break; } } } break; case AncestorResultDataType.DataTable: var rowObjectFactory = objectFactory as Func <DataRow, object>; if (rowObjectFactory == null && factory != null) { rowObjectFactory = row => factory(); } foreach (var item in TableToCollection(result.ReturnDataTable, dataType, rowObjectFactory, firstOnly, mode, hardwordEncoding)) { list.Add(item); } break; } return(list); }
public static IList ResultList(IAncestorResult result, Type[] dataTypes, Delegate objectFactory, ResultListMode mode, Encoding hardwordEncoding) { return(InternalResultList(result, dataTypes, objectFactory, false, mode, hardwordEncoding)); }