Пример #1
0
        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);
        }
Пример #2
0
 public override object ConvertTo(object value)
 {
     this.Validate(value);
     if (!TypeResolver.IsSpecialValueType(base.Type))
     {
         if (ObjectConverter.IsNullOrEmpty(value))
         {
             if (TypeResolver.IsNullable(base.Type))
             {
                 return(null);
             }
         }
         else
         {
             if (TypeResolver.IsEnum(base.Type))
             {
                 return(Enum.Parse(base.Type, value.ToString()));
             }
             if (TypeResolver.IsTimeSpan(base.Type))
             {
                 return(TimeSpan.Parse(value.ToString()));
             }
             this.UpdateType();
             TypeConverter converter = TypeDescriptor.GetConverter(base.Type);
             if (converter.CanConvertTo(base.Type))
             {
                 return(converter.ConvertTo(value, base.Type));
             }
             IConvertible con = value as IConvertible;
             if (con != null)
             {
                 return(con.ToType(base.Type, CultureInfo.CurrentCulture));
             }
         }
         throw new Exception(string.Format("{0}类型无法转换成{1}类型", value.GetType().Name, base.Type.Name));
     }
     if (!ObjectConverter.IsNullOrEmpty(value))
     {
         return(value.ToString().Trim());
     }
     return(value);
 }