예제 #1
0
        /// <summary>
        /// 设置属性值
        /// </summary>
        /// <param name="context">转换上下文</param>
        /// <param name="target">属性实例对象</param>
        /// <param name="value">属性值</param>
        /// <returns></returns>
        public bool SetValue(ConvertContext context, object target, object value)
        {
            if (Set == null)
            {
                context.AddException($"{Property.ReflectedType}.{Property.Name}属性没有set");
                return(false);
            }
            bool b;
            var  v = context.Get(PropertyType).ChangeType(context, value, PropertyType, out b);

            if (b == false)
            {
                context.AddException($"{Property.ReflectedType}属性{Property.Name}值转换失败");
                return(false);
            }
            try
            {
                Set(target, v);
                return(true);
            }
            catch (Exception ex)
            {
                context.AddException(ex);
                return(false);
            }
        }
예제 #2
0
 /// <summary>
 /// 返回指定类型的对象,其值等效于指定对象。
 /// </summary>
 /// <typeparam name="T"> 换转后的类型 </typeparam>
 /// <param name="input"> 需要转换类型的对象 </param>
 /// <param name="success"> 是否成功 </param>
 public static T To <T>(this object input, out bool success)
 {
     using (var context = new ConvertContext())
     {
         var conv = context.Get <T>();
         return(conv.ChangeType(context, input, typeof(T), out success));
     }
 }
예제 #3
0
 /// <summary>
 /// 返回指定类型的对象,其值等效于指定对象。
 /// </summary>
 /// <param name="input"> 需要转换类型的对象 </param>
 /// <param name="outputType"> 换转后的类型 </param>
 /// <param name="success"> 是否成功 </param>
 public static object ChangeType(this object input, Type outputType, out bool success)
 {
     using (var context = new ConvertContext())
     {
         var conv = context.Get(outputType);
         return(conv.ChangeType(context, input, outputType, out success));
     }
 }
예제 #4
0
        protected override MyClass ChangeType(ConvertContext context, string input, Type outputType, out bool success)
        {
            var i = context.Get <int>().ChangeType(context, input, typeof(int), out success);

            return(success ? new MyClass()
            {
                Number = i
            } : null);
        }
예제 #5
0
 /// <summary>
 /// 返回一个指定类型的对象,该对象的值等效于指定的对象。转换失败返回默认值
 /// </summary>
 /// <param name="input"> 需要转换类型的对象 </param>
 /// <param name="outputType"> 要返回的对象的类型 </param>
 /// <param name="defaultValue"> 转换失败时返回的默认值 </param>
 public static object ChangeType(this object input, Type outputType, object defaultValue)
 {
     using (var context = new ConvertContext())
     {
         var  conv = context.Get(outputType);
         bool success;
         var  result = conv.ChangeType(context, input, outputType, out success);
         return(success ? result : defaultValue);
     }
 }
예제 #6
0
 /// <summary>
 /// 尝试对指定对象进行类型转换,返回是否转换成功
 /// </summary>
 /// <param name="input">需要转换类型的对象</param>
 /// <param name="outputType">转换后的类型</param>
 /// <param name="result"></param>
 /// <returns></returns>
 public static bool TryChangedType(object input, Type outputType, out object result)
 {
     using (var context = new ConvertContext())
     {
         bool b;
         var  conv = context.Get(outputType);
         result = conv.ChangeType(context, input, outputType, out b);
         return(b);
     }
 }
예제 #7
0
 /// <summary>
 /// 返回一个指定类型的对象,该对象的值等效于指定的对象。转换失败返回默认值
 /// </summary>
 /// <typeparam name="T"> 要返回的对象类型的泛型 </typeparam>
 /// <param name="input"> 需要转换类型的对象 </param>
 /// <param name="defaultValue"> 转换失败时返回的默认值 </param>
 public static T To <T>(this object input, T defaultValue)
 {
     using (var context = new ConvertContext())
     {
         var  conv = context.Get <T>();
         bool success;
         var  result = conv.ChangeType(context, input, typeof(T), out success);
         return(success ? result : defaultValue);
     }
 }
 /// <summary>
 /// 使用指定的上下文和区域性信息将给定的对象转换为此转换器的类型。
 /// </summary>
 /// <returns>表示转换的 value 的 <see cref="T:System.Object" />。</returns>
 /// <param name="context">一个 <see cref="T:System.ComponentModel.ITypeDescriptorContext" />,提供格式上下文。</param>
 /// <param name="culture">用作当前区域性的 <see cref="T:System.Globalization.CultureInfo" />。</param>
 /// <param name="value">要转换的 <see cref="T:System.Object" />。</param>
 /// <exception cref="InvalidCastException"> 转换失败 </exception>
 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
 {
     using (var ctx = new ConvertContext())
     {
         bool success;
         var  result = _convertor.ChangeType(ctx, value, _convertor.OutputType, out success); if (success == false)
         {
             ctx.ThrowIfHaveError();
         }
         return(result);
     }
 }
예제 #9
0
 /// <summary>
 /// 返回一个指定类型的对象,该对象的值等效于指定的对象。转换失败抛出异常
 /// </summary>
 /// <typeparam name="T"> 要返回的对象类型的泛型 </typeparam>
 /// <param name="input"> 需要转换类型的对象 </param>
 public static T To <T>(this object input)
 {
     using (var context = new ConvertContext())
     {
         var  conv = context.Get <T>();
         bool success;
         var  result = conv.ChangeType(context, input, typeof(T), out success);
         if (success == false)
         {
             context.ThrowIfHaveError();
         }
         return(result);
     }
 }
예제 #10
0
 /// <summary>
 /// 返回一个指定类型的对象,该对象的值等效于指定的对象。转换失败抛出异常
 /// </summary>
 /// <param name="input"> 需要转换类型的对象 </param>
 /// <param name="outputType"> 要返回的对象的类型 </param>
 public static object ChangeType(this object input, Type outputType)
 {
     using (var context = new ConvertContext())
     {
         var  conv = context.Get(outputType);
         bool success;
         var  result = conv.ChangeType(context, input, outputType, out success);
         if (success == false)
         {
             context.ThrowIfHaveError();
         }
         return(result);
     }
 }