Ejemplo n.º 1
0
        public override object ConvertTo(object value)
        {
            this.Validate(value);
            Type[] arrayType = base.Type.GetGenericArguments();
            if (arrayType.Length != 2)
            {
                throw new Exception(string.Format("类型{0}不是Dictionary泛型", base.Type.Name));
            }
            Type       typeKey     = arrayType[0];
            Type       typeValue   = arrayType[1];
            ObjectType objTypeEnum = TypeResolver.GetObjectType(typeKey);

            if (objTypeEnum != ObjectType.Value)
            {
                throw new Exception(string.Format("字典类型{0}的键类型不是值类型", base.Type.Name));
            }
            Dictionary <string, object> dic = value as Dictionary <string, object>;

            if (dic == null)
            {
                return(null);
            }
            IDictionary     dicResult      = Activator.CreateInstance(base.Type) as IDictionary;
            ObjectConverter keyConverter   = ObjectConverterFactory.GetObjectConverter(typeKey, null);
            ObjectConverter valueConverter = ObjectConverterFactory.GetObjectConverter(typeValue, null);

            foreach (KeyValuePair <string, object> kv in dic)
            {
                object keyResult   = keyConverter.ConvertTo(kv.Key);
                object valueResult = valueConverter.ConvertTo(kv.Value);
                dicResult.Add(keyResult, valueResult);
            }
            return(dicResult);
        }
Ejemplo n.º 2
0
        public override object ConvertTo(object value)
        {
            this.Validate(value);
            Type type = base.Type.GetGenericArguments().FirstOrDefault <Type>();

            if (type == null)
            {
                throw new Exception(string.Format("类型{0}不是List泛型", base.Type.Name));
            }
            IList list = value as IList;

            if (list == null)
            {
                return(null);
            }
            IList           listResult = Activator.CreateInstance(base.Type) as IList;
            ObjectConverter converter  = ObjectConverterFactory.GetObjectConverter(type, null);

            foreach (object obj in list)
            {
                object result = converter.ConvertTo(obj);
                listResult.Add(result);
            }
            return(listResult);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        public override object ConvertTo(object value)
        {
            this.Validate(value);
            IList array = value as IList;

            if (array == null)
            {
                return(null);
            }
            IList           arrayConvert = Array.CreateInstance(base.Type, array.Count);
            Type            elementType  = base.Type.GetElementType();
            ObjectConverter converter    = ObjectConverterFactory.GetObjectConverter(elementType, null);

            foreach (object obj in array)
            {
                object objConvert = converter.ConvertTo(obj);
                arrayConvert.Add(objConvert);
            }
            return(arrayConvert);
        }
Ejemplo n.º 5
0
        public override object ConvertTo(Type type, CultureInfo culture)
        {
            ObjectConverter converter = ObjectConverterFactory.GetObjectConverter(type, null);

            return(converter.ConvertTo(base.RawValue));
        }