コード例 #1
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);
 }
コード例 #2
0
ファイル: ArrayConverter.cs プロジェクト: dongwenbing/xili
        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);
        }
コード例 #3
0
        public override object ConvertTo(Type type, CultureInfo culture)
        {
            ObjectConverter converter = ObjectConverterFactory.GetObjectConverter(type, null);

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