public static IList Map(Type objectType, DataSet ds) { var list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(objectType)); Dictionary <string, Action <object, object> > propertySetters = ExpressionReflector.GetSetters(objectType); var properties = ExpressionReflector.GetProperties(objectType); DataTable table = ds.Tables[0]; foreach (DataRow row in ds.Tables[0].Rows) { object local = null; if (objectType.IsSealed) { var parameterObjects = new List <object>(); foreach (DataColumn column in ds.Tables[0].Columns) { var obj = row[column]; if (obj == DBNull.Value) { obj = null; } parameterObjects.Add(obj); } local = ExpressionReflector.CreateInstance(objectType, parameterObjects.ToArray()); } else { local = ExpressionReflector.CreateInstance(objectType); foreach (DataColumn column in ds.Tables[0].Columns) { object obj2 = row[column]; if (obj2 != DBNull.Value) { var property = properties.Get(column.ColumnName); if (property == null) { continue; } Type propertyType = property.PropertyType; Type underlyingType = Nullable.GetUnderlyingType(propertyType); if (underlyingType == null) { underlyingType = propertyType; } if (underlyingType.IsEnum) { obj2 = Enum.Parse(underlyingType, Convert.ToString(obj2)); } else { obj2 = Convert.ChangeType(obj2, underlyingType); } propertySetters.Get(column.ColumnName)(local, obj2); } } } list.Add(local); } return(list); }
public BuilderFactory CreateSqlBuilderFactory() { if (!string.IsNullOrWhiteSpace(ConfigManager.SqlBuilder)) { if (_type == null) { var assInfos = ConfigManager.SqlBuilder.Split(','); _type = Assembly.Load(assInfos[1]).GetTypes().FirstOrDefault(x => x.FullName == assInfos[0]); } return((BuilderFactory)ExpressionReflector.CreateInstance(_type)); } return(new BuilderFactory(DatabaseType)); }