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 PropertyMapping[] GetSetterMappings(Type type, IDataReader reader, string mappingKey = null) { PropertyMapping[] mappings = null; FastType reflection = FastType.Get(type); List <PropertyMapping> list = new List <PropertyMapping>(); for (int i = 0; i < reader.FieldCount; i++) { string columnName = reader.GetName(i); FastProperty prop = reflection.Setters.SingleOrDefault(m => MatchColumnName(m.Name, columnName)); if (prop != null) { list.Add(new PropertyMapping() { Prop = prop, Index = i }); } } mappings = list.ToArray(); return(mappings); }
protected virtual void InitializeProperties() { List <FastProperty> setters = new List <FastProperty>(); List <FastProperty> getters = new List <FastProperty>(); foreach (PropertyInfo prop in GetProperties(_type)) { String columnName = GetPropertyName(prop); FastProperty mapping = new FastProperty(columnName, prop); if (prop.CanRead) { getters.Add(mapping); } if (prop.CanWrite) { setters.Add(mapping); } } _setters = setters.ToArray(); _getters = getters.ToArray(); }
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)); } }