ConvertFrom() public method

public ConvertFrom ( ITypeDescriptorContext context, CultureInfo culture, object value ) : object
context ITypeDescriptorContext
culture System.Globalization.CultureInfo
value object
return object
Exemplo n.º 1
0
        private TypeMath(Type valueType)
        {
            if (!TypeInfo.IsNumericType(valueType))
                throw new Exception("Numeric type is required for math!");

            Min = TypeMathExpressionHelper.GetMinDelegate<BinOp>(valueType, typeof(object));
            Max = TypeMathExpressionHelper.GetMaxDelegate<BinOp>(valueType, typeof(object));

            Sum = TypeMathExpressionHelper.GetSumDelegate<BinOp>(valueType, typeof(object));
            Multiply = TypeMathExpressionHelper.GetMultiplyDelegate<BinOp>(valueType, typeof(object));

            if (TypeInfo.IsNullableType(valueType))
            {
                var nc = new NullableConverter(valueType);
                Zero = nc.ConvertFrom(Convert.ChangeType(0, Nullable.GetUnderlyingType(valueType)));
                One = nc.ConvertFrom(Convert.ChangeType(1, Nullable.GetUnderlyingType(valueType)));
                SumNullAsZero = TypeMathExpressionHelper.GetSumIsNullDelegate<BinOp>(valueType, typeof(object), Zero);
                MultiplyNullAsOne = TypeMathExpressionHelper.GetMultiplyIsNullDelegate<BinOp>(valueType, typeof(object), One);
            }
            else
            {
                Zero = Convert.ChangeType(0, valueType);
                One = Convert.ChangeType(1, valueType);
                SumNullAsZero = Sum;
                MultiplyNullAsOne = Multiply;
            }
        }
Exemplo n.º 2
0
 protected object ConvertFrom(object value, Type t)
 {
     if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
     {
         var nc = new NullableConverter(t);
         return nc.ConvertFrom(value);
     }
     return Convert.ChangeType(value, t);
 }
Exemplo n.º 3
0
        public static object ChangeType(object o, Type type)
        {
            if (type.IsEnum)
                return Enum.Parse(type, o.ToString(), true);

            if (Nullable.IsNullableType(type))
            {
                var underlyingType = Nullable.GetUnderlyingType(type);
                var data = ChangeType(o, underlyingType);
                NullableConverter nullableConverter = new NullableConverter(type);
                return nullableConverter.ConvertFrom(data);
            }
            return System.Convert.ChangeType(o, type);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Sets the new value for a specific property on an object.
        /// </summary>
        /// <param name="instance">Target object to set property at.</param>
        /// <param name="pi">Property info for the property to set.</param>
        /// <param name="newvalue">Value to try to parse.</param>
        /// <param name="culture"></param>
        public void SetValue(object instance, PropertyInfo pi, object newvalue, CultureInfo culture = null)
        {
            var nullableConverter = new NullableConverter(pi.PropertyType);
            object obj;
            try
            {
                obj = nullableConverter.ConvertFrom(null, culture ?? CultureInfo.CurrentCulture, newvalue);
            }
            catch (Exception)
            {
                ValidationManager.AddError(pi, "Adf.Business.NotInstantiable", newvalue, pi.Name);
                return;
            }

            pi.SetValue(instance, obj, null);
        }
Exemplo n.º 5
0
        private static object ConvertEnumToNullableType(ResolutionContext context)
        {
            var nullableConverter = new NullableConverter(context.DestinationType);

            if (context.IsSourceValueNull)
            {
                return nullableConverter.ConvertFrom(context.SourceValue);
            }

            var destType = nullableConverter.UnderlyingType;
            return Convert.ChangeType(context.SourceValue, destType);
        }
Exemplo n.º 6
0
		public void ConvertFrom_EmptyString ()
		{
			NullableConverter converter = new NullableConverter (typeof(MyType?));
			Assert.IsNull (converter.ConvertFrom (null, null, String.Empty), "#1");
		}