private static T Read <T>(IDataReader reader, Type type, PropertyMapping[] mappings) { object instance = Activator.CreateInstance(type); foreach (PropertyMapping mapping in mappings) { FastProperty prop = mapping.Prop; prop.SetValue(instance, reader.GetValue(mapping.Index).ConvertToType(prop.Type)); } return((T)instance); }
private static void GetCompositeProperty(NameValueCollection source, object instance, FastProperty p, bool needKh, string prefix = "") { if (p.Type.IsGenericType) { var pType = p.Type.GetGenericArguments()[0]; var t = typeof(List <>); t = t.MakeGenericType(pType); var listInstance = Activator.CreateInstance(t); int i = 0; string keyTemplate = needKh ? "{0}[{1}][{2}]" : "{0}{1}[{2}]"; while (source.AllKeys.Any(o => o.StartsWith(keyTemplate.FormatTo(prefix, p.Name, i)))) { string key = keyTemplate.FormatTo(prefix, p.Name, i); object pInstance = null; if (pType.IsInterface) { pInstance = ObjectHelper.GetObject(pType); } else { pInstance = Activator.CreateInstance(pType); } var pInstanceType = FastType.Get(pInstance.GetType()); foreach (var pp in pInstanceType.Setters) { if (pp.Type == typeof(string) || pp.Type.IsValueType) { string tempKey = "{0}[{1}]".FormatTo(key, pp.Name); if (source.AllKeys.Contains(tempKey)) { pp.SetValue(pInstance, source[tempKey].ConvertToType(pp.Type)); } } else { GetCompositeProperty(source, pInstance, pp, true, key); } } var add = listInstance.GetType().GetMethod("Add"); add.Invoke(listInstance, new object[] { pInstance }); i++; } p.SetValue(instance, listInstance.ConvertToType(p.Type)); } }