public override object ConvertTo(object value) { this.Validate(value); Dictionary <string, object> dic = value as Dictionary <string, object>; if (dic == null) { return(null); } dic = SingleClassConverter.IgnoreCase(dic); object obj = Activator.CreateInstance(base.Type); ObjectValidaterFactory validaterFactory = new ObjectValidaterFactory(base.Type); PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(base.Type); foreach (PropertyDescriptor pd in pdc) { object subValue = null; dic.TryGetValue(pd.Name, out subValue); ObjectValidater validater = validaterFactory.GetObjectValidater(pd); if (!ObjectConverter.IsNullOrEmpty(subValue) || validater != null) { ObjectConverter converter = ObjectConverterFactory.GetObjectConverter(pd.PropertyType, validater); object result = converter.ConvertTo(subValue); pd.SetValue(obj, result); } } if (obj is IBindModelCallback) { (obj as IBindModelCallback).OnBindModel(); } return(obj); }
public static ObjectConverter GetObjectConverter(Type type, ObjectValidater validater) { switch (TypeResolver.GetObjectType(type)) { case ObjectType.Value: return(new ValueTypeConverter(type, validater)); case ObjectType.SingleClass: return(new SingleClassConverter(type, validater)); case ObjectType.Array: return(new ArrayConverter(type, validater)); case ObjectType.GenericList: return(new GenericListConverter(type, validater)); case ObjectType.GenericDictionary: return(new GenericDictionaryConverter(type, validater)); default: throw new Exception(string.Format("类型转换器未指定类型{0}", type.Name)); } }
public ValueTypeConverter(Type type, ObjectValidater validater) { base.Type = type; base.Validater = validater; }
public GenericDictionaryConverter(Type type, ObjectValidater validater) { base.Type = type; base.Validater = validater; }
public ArrayConverter(Type type, ObjectValidater validater) { base.Type = type; base.Validater = validater; }
public GenericListConverter(Type type, ObjectValidater validater) { base.Type = type; base.Validater = validater; }
public SingleClassConverter(Type type, ObjectValidater validater) { base.Type = type; base.Validater = validater; }