Esempio n. 1
0
        /// <summary>
        /// Convert passed in value into the specified type when property is registered.
        /// </summary>
        /// <returns>Converted object</returns>
        private static T ConvertPropertyTo <T>(TestProperty property, CultureInfo culture, object value)
        {
            ValidateArg.NotNull(property, "property");
            ValidateArg.NotNull(culture, "culture");

            var lazyValue = value as LazyPropertyValue <T>;

            if (value == null)
            {
                return(default(T));
            }
            else if (value is T)
            {
                return((T)value);
            }
            else if (lazyValue != null)
            {
                return(lazyValue.Value);
            }

            var valueType = property.GetValueType();

            TypeConverter converter = TypeDescriptor.GetConverter(valueType);

            if (converter == null)
            {
                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Resources.Resources.ConverterNotSupported, valueType.Name));
            }

            try
            {
                return((T)converter.ConvertTo(null, culture, value, typeof(T)));
            }
            catch (FormatException)
            {
                throw;
            }
            catch (Exception e)
            {
                // some type converters throw strange exceptions (eg: System.Exception by Int32Converter)
                throw new FormatException(e.Message, e);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Convert passed in value from TestProperty's specified value type.
        /// </summary>
        /// <returns>Converted object</returns>
        private static object ConvertPropertyFrom <T>(TestProperty property, CultureInfo culture, object value)
        {
            ValidateArg.NotNull(property, "property");
            ValidateArg.NotNull(culture, "culture");

            var valueType = property.GetValueType();

            // Do not try conversion if the object is already of the type we're trying to convert.
            // Note that typeof(T) may be object in case the value is getting deserialized via the StoreKvpList, however
            // the deserializer could have converted it already, hence the runtime type check.
            if (valueType != null && (valueType.GetTypeInfo().IsAssignableFrom(typeof(T).GetTypeInfo()) || valueType.GetTypeInfo().IsAssignableFrom(value?.GetType().GetTypeInfo())))
            {
                return(value);
            }

            TypeConverter converter = TypeDescriptor.GetConverter(valueType);

            if (converter == null)
            {
                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Resources.Resources.ConverterNotSupported, valueType.Name));
            }

            try
            {
                return(converter.ConvertFrom(null, culture, value));
            }
            catch (FormatException)
            {
                throw;
            }
            catch (Exception e)
            {
                // some type converters throw strange exceptions (eg: System.Exception by Int32Converter)
                throw new FormatException(e.Message, e);
            }
        }