private static ConvertResult TryConvertInternal(object initialValue, CultureInfo culture, Type targetType, out object value)
        {
            if (initialValue == null)
            {
                throw new ArgumentNullException("initialValue");
            }

            if (ReflectionUtils.IsNullableType(targetType))
            {
                targetType = Nullable.GetUnderlyingType(targetType);
            }

            Type initialType = initialValue.GetType();

            if (targetType == initialType)
            {
                value = initialValue;
                return(ConvertResult.Success);
            }

            // use Convert.ChangeType if both types are IConvertible
            if (ConvertUtils.IsConvertible(initialValue.GetType()) && ConvertUtils.IsConvertible(targetType))
            {
                if (targetType.IsEnum())
                {
                    if (initialValue is string)
                    {
                        value = Enum.Parse(targetType, initialValue.ToString(), true);
                        return(ConvertResult.Success);
                    }
                    else if (IsInteger(initialValue))
                    {
                        value = Enum.ToObject(targetType, initialValue);
                        return(ConvertResult.Success);
                    }
                }

                value = System.Convert.ChangeType(initialValue, targetType, culture);
                return(ConvertResult.Success);
            }

#if !NET20
            if (initialValue is DateTime && targetType == typeof(DateTimeOffset))
            {
                value = new DateTimeOffset((DateTime)initialValue);
                return(ConvertResult.Success);
            }
#endif

            if (initialValue is byte[] && targetType == typeof(Guid))
            {
                value = new Guid((byte[])initialValue);
                return(ConvertResult.Success);
            }

            if (initialValue is Guid && targetType == typeof(byte[]))
            {
                value = ((Guid)initialValue).ToByteArray();
                return(ConvertResult.Success);
            }

            string s = initialValue as string;
            if (s != null)
            {
                if (targetType == typeof(Guid))
                {
                    value = new Guid(s);
                    return(ConvertResult.Success);
                }
                if (targetType == typeof(Uri))
                {
                    value = new Uri(s, UriKind.RelativeOrAbsolute);
                    return(ConvertResult.Success);
                }
                if (targetType == typeof(TimeSpan))
                {
                    value = ParseTimeSpan(s);
                    return(ConvertResult.Success);
                }
                if (targetType == typeof(byte[]))
                {
                    value = System.Convert.FromBase64String(s);
                    return(ConvertResult.Success);
                }
                if (targetType == typeof(Version))
                {
                    Version result;
                    if (VersionTryParse(s, out result))
                    {
                        value = result;
                        return(ConvertResult.Success);
                    }
                    value = null;
                    return(ConvertResult.NoValidConversion);
                }
                if (typeof(Type).IsAssignableFrom(targetType))
                {
                    value = Type.GetType(s, true);
                    return(ConvertResult.Success);
                }
            }

#if !(NET20 || NET35 || PORTABLE40 || PORTABLE)
            if (targetType == typeof(BigInteger))
            {
                value = ToBigInteger(initialValue);
                return(ConvertResult.Success);
            }
            if (initialValue is BigInteger)
            {
                value = FromBigInteger((BigInteger)initialValue, targetType);
                return(ConvertResult.Success);
            }
#endif

#if !(PORTABLE40 || PORTABLE)
            // see if source or target types have a TypeConverter that converts between the two
            TypeConverter toConverter = GetConverter(initialType);

            if (toConverter != null && toConverter.CanConvertTo(targetType))
            {
                value = toConverter.ConvertTo(null, culture, initialValue, targetType);
                return(ConvertResult.Success);
            }

            TypeConverter fromConverter = GetConverter(targetType);

            if (fromConverter != null && fromConverter.CanConvertFrom(initialType))
            {
                value = fromConverter.ConvertFrom(null, culture, initialValue);
                return(ConvertResult.Success);
            }
#endif
#if !(DOTNET || PORTABLE40 || PORTABLE)
            // handle DBNull and INullable
            if (initialValue == DBNull.Value)
            {
                if (ReflectionUtils.IsNullable(targetType))
                {
                    value = EnsureTypeAssignable(null, initialType, targetType);
                    return(ConvertResult.Success);
                }

                // cannot convert null to non-nullable
                value = null;
                return(ConvertResult.CannotConvertNull);
            }
#endif
#if !(DOTNET || PORTABLE40 || PORTABLE)
            if (initialValue is INullable)
            {
                value = EnsureTypeAssignable(ToValue((INullable)initialValue), initialType, targetType);
                return(ConvertResult.Success);
            }
#endif

            if (targetType.IsInterface() || targetType.IsGenericTypeDefinition() || targetType.IsAbstract())
            {
                value = null;
                return(ConvertResult.NotInstantiableType);
            }

            value = null;
            return(ConvertResult.NoValidConversion);
        }
示例#2
0
        /// <summary>
        /// Converts the value to the specified type.
        /// </summary>
        /// <param name="initialValue">The value to convert.</param>
        /// <param name="culture">The culture to use when converting.</param>
        /// <param name="targetType">The type to convert the value to.</param>
        /// <returns>The converted type.</returns>
        public static object Convert(object initialValue, CultureInfo culture, Type targetType)
        {
            if (initialValue == null)
            {
                throw new ArgumentNullException("initialValue");
            }

            if (ReflectionUtils.IsNullableType(targetType))
            {
                targetType = Nullable.GetUnderlyingType(targetType);
            }

            Type initialType = initialValue.GetType();

            if (targetType == initialType)
            {
                return(initialValue);
            }

            if (initialValue is string && typeof(Type).IsAssignableFrom(targetType))
            {
                return(Type.GetType((string)initialValue, true));
            }

            if (targetType.IsInterface || targetType.IsGenericTypeDefinition || targetType.IsAbstract)
            {
                throw new ArgumentException("Target type {0} is not a value type or a non-abstract class.".FormatWith(CultureInfo.InvariantCulture, targetType), "targetType");
            }

            // use Convert.ChangeType if both types are IConvertible
            if (initialValue is IConvertible && typeof(IConvertible).IsAssignableFrom(targetType))
            {
                if (targetType.IsEnum)
                {
                    if (initialValue is string)
                    {
                        return(Enum.Parse(targetType, initialValue.ToString(), true));
                    }
                    else if (IsInteger(initialValue))
                    {
                        return(Enum.ToObject(targetType, initialValue));
                    }
                }

                return(System.Convert.ChangeType(initialValue, targetType, culture));
            }

            if (initialValue is DateTime && targetType == typeof(DateTimeOffset))
            {
                return(new DateTimeOffset((DateTime)initialValue));
            }

            if (initialValue is string)
            {
                if (targetType == typeof(Guid))
                {
                    return(new Guid((string)initialValue));
                }
                if (targetType == typeof(Uri))
                {
                    return(new Uri((string)initialValue));
                }
                if (targetType == typeof(TimeSpan))
                {
                    return(TimeSpan.Parse((string)initialValue));
                }
            }

#if !(UNITY_WP8 || UNITY_WP_8_1)
            // see if source or target types have a TypeConverter that converts between the two
            TypeConverter toConverter = GetConverter(initialType);

            if (toConverter != null && toConverter.CanConvertTo(targetType))
            {
#if !(UNITY_WP8 || UNITY_WP_8_1)
                return(toConverter.ConvertTo(null, culture, initialValue, targetType));
#else
                return(toConverter.ConvertTo(initialValue, targetType));
#endif
            }

            TypeConverter fromConverter = GetConverter(targetType);

            if (fromConverter != null && fromConverter.CanConvertFrom(initialType))
            {
#if !(UNITY_WP8 || UNITY_WP_8_1)
                return(fromConverter.ConvertFrom(null, culture, initialValue));
#else
                return(fromConverter.ConvertFrom(initialValue));
#endif
            }
#endif

            // handle DBNull and INullable
            if (initialValue == DBNull.Value)
            {
                if (ReflectionUtils.IsNullable(targetType))
                {
                    return(EnsureTypeAssignable(null, initialType, targetType));
                }

                throw new Exception("Can not convert null {0} into non-nullable {1}.".FormatWith(CultureInfo.InvariantCulture, initialType, targetType));
            }

            throw new Exception("Can not convert from {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, initialType, targetType));
        }
        public static object GetDefaultValue(Type type)
        {
            if (!type.IsValueType())
            {
                return(null);
            }
            PrimitiveTypeCode typeCode = ConvertUtils.GetTypeCode(type);

            switch (typeCode)
            {
            case PrimitiveTypeCode.Char:
            case PrimitiveTypeCode.SByte:
            case PrimitiveTypeCode.Int16:
            case PrimitiveTypeCode.UInt16:
            case PrimitiveTypeCode.Int32:
            case PrimitiveTypeCode.Byte:
            case PrimitiveTypeCode.UInt32:
                return(0);

            case PrimitiveTypeCode.CharNullable:
            case PrimitiveTypeCode.BooleanNullable:
            case PrimitiveTypeCode.SByteNullable:
            case PrimitiveTypeCode.Int16Nullable:
            case PrimitiveTypeCode.UInt16Nullable:
            case PrimitiveTypeCode.Int32Nullable:
            case PrimitiveTypeCode.ByteNullable:
            case PrimitiveTypeCode.UInt32Nullable:
            case PrimitiveTypeCode.Int64Nullable:
            case PrimitiveTypeCode.UInt64Nullable:
            case PrimitiveTypeCode.SingleNullable:
            case PrimitiveTypeCode.DoubleNullable:
            case PrimitiveTypeCode.DateTimeNullable:
            case PrimitiveTypeCode.DateTimeOffsetNullable:
            case PrimitiveTypeCode.DecimalNullable:
                break;

            case PrimitiveTypeCode.Boolean:
                return(false);

            case PrimitiveTypeCode.Int64:
            case PrimitiveTypeCode.UInt64:
                return(0L);

            case PrimitiveTypeCode.Single:
                return(0f);

            case PrimitiveTypeCode.Double:
                return(0.0);

            case PrimitiveTypeCode.DateTime:
                return(default(DateTime));

            case PrimitiveTypeCode.DateTimeOffset:
                return(default(DateTimeOffset));

            case PrimitiveTypeCode.Decimal:
                return(0m);

            case PrimitiveTypeCode.Guid:
                return(default(Guid));

            default:
                if (typeCode == PrimitiveTypeCode.BigInteger)
                {
                    return(default(BigInteger));
                }
                break;
            }
            if (ReflectionUtils.IsNullable(type))
            {
                return(null);
            }
            return(Activator.CreateInstance(type));
        }
        /// <summary>
        /// Converts the value to the specified type.
        /// </summary>
        /// <param name="initialValue">The value to convert.</param>
        /// <param name="culture">The culture to use when converting.</param>
        /// <param name="targetType">The type to convert the value to.</param>
        /// <returns>The converted type.</returns>
        public static object Convert(object initialValue, CultureInfo culture, Type targetType)
        {
            if (initialValue == null)
            {
                throw new ArgumentNullException("initialValue");
            }

            if (ReflectionUtils.IsNullableType(targetType))
            {
                targetType = Nullable.GetUnderlyingType(targetType);
            }

            Type initialType = initialValue.GetType();

            if (targetType == initialType)
            {
                return(initialValue);
            }

            // use Convert.ChangeType if both types are IConvertible
            if (ConvertUtils.IsConvertible(initialValue.GetType()) && ConvertUtils.IsConvertible(targetType))
            {
                if (targetType.IsEnum())
                {
                    if (initialValue is string)
                    {
                        return(Enum.Parse(targetType, initialValue.ToString(), true));
                    }
                    else if (IsInteger(initialValue))
                    {
                        return(Enum.ToObject(targetType, initialValue));
                    }
                }

                return(System.Convert.ChangeType(initialValue, targetType, culture));
            }

#if !NET20
            if (initialValue is DateTime && targetType == typeof(DateTimeOffset))
            {
                return(new DateTimeOffset((DateTime)initialValue));
            }
#endif

            if (initialValue is byte[] && targetType == typeof(Guid))
            {
                return(new Guid((byte[])initialValue));
            }

            if (initialValue is string)
            {
                if (targetType == typeof(Guid))
                {
                    return(new Guid((string)initialValue));
                }
                if (targetType == typeof(Uri))
                {
                    return(new Uri((string)initialValue, UriKind.RelativeOrAbsolute));
                }
                if (targetType == typeof(TimeSpan))
                {
                    return(ParseTimeSpan((string)initialValue));
                }
                if (typeof(Type).IsAssignableFrom(targetType))
                {
                    return(Type.GetType((string)initialValue, true));
                }
            }

#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE)
            if (targetType == typeof(BigInteger))
            {
                return(ToBigInteger(initialValue));
            }
            if (initialValue is BigInteger)
            {
                return(FromBigInteger((BigInteger)initialValue, targetType));
            }
#endif

#if !(NETFX_CORE || PORTABLE40 || PORTABLE)
            // see if source or target types have a TypeConverter that converts between the two
            TypeConverter toConverter = GetConverter(initialType);

            if (toConverter != null && toConverter.CanConvertTo(targetType))
            {
#if !SILVERLIGHT
                return(toConverter.ConvertTo(null, culture, initialValue, targetType));
#else
                return(toConverter.ConvertTo(initialValue, targetType));
#endif
            }

            TypeConverter fromConverter = GetConverter(targetType);

            if (fromConverter != null && fromConverter.CanConvertFrom(initialType))
            {
#if !SILVERLIGHT
                return(fromConverter.ConvertFrom(null, culture, initialValue));
#else
                return(fromConverter.ConvertFrom(initialValue));
#endif
            }
#endif
#if !(NETFX_CORE || PORTABLE40 || PORTABLE)
            // handle DBNull and INullable
            if (initialValue == DBNull.Value)
            {
                if (ReflectionUtils.IsNullable(targetType))
                {
                    return(EnsureTypeAssignable(null, initialType, targetType));
                }

                throw new Exception("Can not convert null {0} into non-nullable {1}.".FormatWith(CultureInfo.InvariantCulture, initialType, targetType));
            }
#endif
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE40 || PORTABLE)
            if (initialValue is INullable)
            {
                return(EnsureTypeAssignable(ToValue((INullable)initialValue), initialType, targetType));
            }
#endif

            if (targetType.IsInterface() || targetType.IsGenericTypeDefinition() || targetType.IsAbstract())
            {
                throw new ArgumentException("Target type {0} is not a value type or a non-abstract class.".FormatWith(CultureInfo.InvariantCulture, targetType), "targetType");
            }

            throw new InvalidOperationException("Can not convert from {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, initialType, targetType));
        }
示例#5
0
        public static bool CanConvertType(Type initialType, Type targetType, bool allowTypeNameToString)
        {
            ValidationUtils.ArgumentNotNull(initialType, "initialType");
            ValidationUtils.ArgumentNotNull(targetType, "targetType");

            if (ReflectionUtils.IsNullableType(targetType))
            {
                targetType = Nullable.GetUnderlyingType(targetType);
            }

            if (targetType == initialType)
            {
                return(true);
            }

            if (typeof(IConvertible).IsAssignableFrom(initialType) && typeof(IConvertible).IsAssignableFrom(targetType))
            {
                return(true);
            }

            if (initialType == typeof(DateTime) && targetType == typeof(DateTimeOffset))
            {
                return(true);
            }

            if (initialType == typeof(Guid) && (targetType == typeof(Guid) || targetType == typeof(string)))
            {
                return(true);
            }

            if (initialType == typeof(Type) && targetType == typeof(string))
            {
                return(true);
            }

#if !(UNITY_WP8 || UNITY_WP_8_1)
            // see if source or target types have a TypeConverter that converts between the two
            TypeConverter toConverter = GetConverter(initialType);

            if (toConverter != null && !IsComponentConverter(toConverter) && toConverter.CanConvertTo(targetType))
            {
                if (allowTypeNameToString || toConverter.GetType() != typeof(TypeConverter))
                {
                    return(true);
                }
            }

            TypeConverter fromConverter = GetConverter(targetType);

            if (fromConverter != null && !IsComponentConverter(fromConverter) && fromConverter.CanConvertFrom(initialType))
            {
                return(true);
            }
#endif

            // handle DBNull and INullable
            if (initialType == typeof(DBNull))
            {
                if (ReflectionUtils.IsNullable(targetType))
                {
                    return(true);
                }
            }

            return(false);
        }
示例#6
0
        public static object Convert(object initialValue, CultureInfo culture, Type targetType)
        {
            if (initialValue == null)
            {
                throw new ArgumentNullException("initialValue");
            }
            if (ReflectionUtils.IsNullableType(targetType))
            {
                targetType = Nullable.GetUnderlyingType(targetType);
            }
            Type type = initialValue.GetType();

            if (targetType == type)
            {
                return(initialValue);
            }
            if (ConvertUtils.IsConvertible(initialValue) && ConvertUtils.IsConvertible(targetType))
            {
                if (TypeExtensions.IsEnum(targetType))
                {
                    if (initialValue is string)
                    {
                        return(Enum.Parse(targetType, initialValue.ToString(), true));
                    }
                    if (ConvertUtils.IsInteger(initialValue))
                    {
                        return(Enum.ToObject(targetType, initialValue));
                    }
                }
                return(Convert.ChangeType(initialValue, targetType, (IFormatProvider)culture));
            }
            else
            {
                if (initialValue is string && typeof(Type).IsAssignableFrom(targetType))
                {
                    return((object)Type.GetType((string)initialValue, true));
                }
                if (TypeExtensions.IsInterface(targetType) || TypeExtensions.IsGenericTypeDefinition(targetType) || TypeExtensions.IsAbstract(targetType))
                {
                    throw new ArgumentException(StringUtils.FormatWith("Target type {0} is not a value type or a non-abstract class.", (IFormatProvider)CultureInfo.InvariantCulture, (object)targetType), "targetType");
                }
                if (initialValue is string)
                {
                    if (targetType == typeof(Guid))
                    {
                        return((object)new Guid((string)initialValue));
                    }
                    if (targetType == typeof(Uri))
                    {
                        return((object)new Uri((string)initialValue, UriKind.RelativeOrAbsolute));
                    }
                    if (targetType == typeof(TimeSpan))
                    {
                        return((object)TimeSpan.Parse((string)initialValue));
                    }
                }
                TypeConverter converter1 = ConvertUtils.GetConverter(type);
                if (converter1 != null && converter1.CanConvertTo(targetType))
                {
                    return(converter1.ConvertTo((ITypeDescriptorContext)null, culture, initialValue, targetType));
                }
                TypeConverter converter2 = ConvertUtils.GetConverter(targetType);
                if (converter2 != null && converter2.CanConvertFrom(type))
                {
                    return(converter2.ConvertFrom((ITypeDescriptorContext)null, culture, initialValue));
                }
                if (initialValue == DBNull.Value)
                {
                    if (ReflectionUtils.IsNullable(targetType))
                    {
                        return(ConvertUtils.EnsureTypeAssignable((object)null, type, targetType));
                    }
                    else
                    {
                        throw new Exception(StringUtils.FormatWith("Can not convert null {0} into non-nullable {1}.", (IFormatProvider)CultureInfo.InvariantCulture, (object)type, (object)targetType));
                    }
                }
                else if (initialValue is INullable)
                {
                    return(ConvertUtils.EnsureTypeAssignable(ConvertUtils.ToValue((INullable)initialValue), type, targetType));
                }
                else
                {
                    throw new InvalidOperationException(StringUtils.FormatWith("Can not convert from {0} to {1}.", (IFormatProvider)CultureInfo.InvariantCulture, (object)type, (object)targetType));
                }
            }
        }
示例#7
0
        private static ConvertUtils.ConvertResult TryConvertInternal(object initialValue, CultureInfo culture, Type targetType, out object value)
        {
            Version version;

            if (initialValue == null)
            {
                throw new ArgumentNullException("initialValue");
            }
            if (ReflectionUtils.IsNullableType(targetType))
            {
                targetType = Nullable.GetUnderlyingType(targetType);
            }
            Type type = initialValue.GetType();

            if (targetType == type)
            {
                value = initialValue;
                return(ConvertUtils.ConvertResult.Success);
            }
            if (ConvertUtils.IsConvertible(initialValue.GetType()) && ConvertUtils.IsConvertible(targetType))
            {
                if (targetType.IsEnum())
                {
                    if (initialValue is string)
                    {
                        value = Enum.Parse(targetType, initialValue.ToString(), true);
                        return(ConvertUtils.ConvertResult.Success);
                    }
                    if (ConvertUtils.IsInteger(initialValue))
                    {
                        value = Enum.ToObject(targetType, initialValue);
                        return(ConvertUtils.ConvertResult.Success);
                    }
                }
                value = Convert.ChangeType(initialValue, targetType, culture);
                return(ConvertUtils.ConvertResult.Success);
            }
            object obj  = initialValue;
            object obj1 = obj;

            if (obj is DateTime)
            {
                DateTime dateTime = (DateTime)obj1;
                if (targetType == typeof(DateTimeOffset))
                {
                    value = new DateTimeOffset(dateTime);
                    return(ConvertUtils.ConvertResult.Success);
                }
            }
            byte[] numArray  = initialValue as byte[];
            byte[] numArray1 = numArray;
            if (numArray != null && targetType == typeof(Guid))
            {
                value = new Guid(numArray1);
                return(ConvertUtils.ConvertResult.Success);
            }
            object obj2 = initialValue;

            obj1 = obj2;
            if (obj2 is Guid)
            {
                Guid guid = (Guid)obj1;
                if (targetType == typeof(byte[]))
                {
                    value = guid.ToByteArray();
                    return(ConvertUtils.ConvertResult.Success);
                }
            }
            string str  = initialValue as string;
            string str1 = str;

            if (str != null)
            {
                if (targetType == typeof(Guid))
                {
                    value = new Guid(str1);
                    return(ConvertUtils.ConvertResult.Success);
                }
                if (targetType == typeof(Uri))
                {
                    value = new Uri(str1, UriKind.RelativeOrAbsolute);
                    return(ConvertUtils.ConvertResult.Success);
                }
                if (targetType == typeof(TimeSpan))
                {
                    value = ConvertUtils.ParseTimeSpan(str1);
                    return(ConvertUtils.ConvertResult.Success);
                }
                if (targetType == typeof(byte[]))
                {
                    value = Convert.FromBase64String(str1);
                    return(ConvertUtils.ConvertResult.Success);
                }
                if (targetType == typeof(Version))
                {
                    if (Version.TryParse(str1, out version))
                    {
                        value = version;
                        return(ConvertUtils.ConvertResult.Success);
                    }
                    value = null;
                    return(ConvertUtils.ConvertResult.NoValidConversion);
                }
                if (typeof(Type).IsAssignableFrom(targetType))
                {
                    value = Type.GetType(str1, true);
                    return(ConvertUtils.ConvertResult.Success);
                }
            }
            if (targetType == typeof(BigInteger))
            {
                value = ConvertUtils.ToBigInteger(initialValue);
                return(ConvertUtils.ConvertResult.Success);
            }
            object obj3 = initialValue;

            obj1 = obj3;
            if (obj3 is BigInteger)
            {
                value = ConvertUtils.FromBigInteger((BigInteger)obj1, targetType);
                return(ConvertUtils.ConvertResult.Success);
            }
            TypeConverter converter = TypeDescriptor.GetConverter(type);

            if (converter != null && converter.CanConvertTo(targetType))
            {
                value = converter.ConvertTo(null, culture, initialValue, targetType);
                return(ConvertUtils.ConvertResult.Success);
            }
            TypeConverter typeConverter = TypeDescriptor.GetConverter(targetType);

            if (typeConverter != null && typeConverter.CanConvertFrom(type))
            {
                value = typeConverter.ConvertFrom(null, culture, initialValue);
                return(ConvertUtils.ConvertResult.Success);
            }
            if (initialValue == DBNull.Value)
            {
                if (!ReflectionUtils.IsNullable(targetType))
                {
                    value = null;
                    return(ConvertUtils.ConvertResult.CannotConvertNull);
                }
                value = ConvertUtils.EnsureTypeAssignable(null, type, targetType);
                return(ConvertUtils.ConvertResult.Success);
            }
            if (!targetType.IsInterface() && !targetType.IsGenericTypeDefinition() && !targetType.IsAbstract())
            {
                value = null;
                return(ConvertUtils.ConvertResult.NoValidConversion);
            }
            value = null;
            return(ConvertUtils.ConvertResult.NotInstantiableType);
        }
示例#8
0
        // Token: 0x06000CFA RID: 3322 RVA: 0x0004A7F8 File Offset: 0x000489F8
        private static ConvertUtils.ConvertResult TryConvertInternal([Nullable(2)] object initialValue, CultureInfo culture, Type targetType, [Nullable(2)] out object value)
        {
            if (initialValue == null)
            {
                throw new ArgumentNullException("initialValue");
            }
            if (ReflectionUtils.IsNullableType(targetType))
            {
                targetType = Nullable.GetUnderlyingType(targetType);
            }
            Type type = initialValue.GetType();

            if (targetType == type)
            {
                value = initialValue;
                return(ConvertUtils.ConvertResult.Success);
            }
            if (ConvertUtils.IsConvertible(initialValue.GetType()) && ConvertUtils.IsConvertible(targetType))
            {
                if (targetType.IsEnum())
                {
                    if (initialValue is string)
                    {
                        value = Enum.Parse(targetType, initialValue.ToString(), true);
                        return(ConvertUtils.ConvertResult.Success);
                    }
                    if (ConvertUtils.IsInteger(initialValue))
                    {
                        value = Enum.ToObject(targetType, initialValue);
                        return(ConvertUtils.ConvertResult.Success);
                    }
                }
                value = System.Convert.ChangeType(initialValue, targetType, culture);
                return(ConvertUtils.ConvertResult.Success);
            }
            if (initialValue is DateTime)
            {
                DateTime dateTime = (DateTime)initialValue;
                if (targetType == typeof(DateTimeOffset))
                {
                    value = new DateTimeOffset(dateTime);
                    return(ConvertUtils.ConvertResult.Success);
                }
            }
            byte[] array = initialValue as byte[];
            if (array != null && targetType == typeof(Guid))
            {
                value = new Guid(array);
                return(ConvertUtils.ConvertResult.Success);
            }
            if (initialValue is Guid)
            {
                Guid guid = (Guid)initialValue;
                if (targetType == typeof(byte[]))
                {
                    value = guid.ToByteArray();
                    return(ConvertUtils.ConvertResult.Success);
                }
            }
            string text = initialValue as string;

            if (text != null)
            {
                if (targetType == typeof(Guid))
                {
                    value = new Guid(text);
                    return(ConvertUtils.ConvertResult.Success);
                }
                if (targetType == typeof(Uri))
                {
                    value = new Uri(text, UriKind.RelativeOrAbsolute);
                    return(ConvertUtils.ConvertResult.Success);
                }
                if (targetType == typeof(TimeSpan))
                {
                    value = ConvertUtils.ParseTimeSpan(text);
                    return(ConvertUtils.ConvertResult.Success);
                }
                if (targetType == typeof(byte[]))
                {
                    value = System.Convert.FromBase64String(text);
                    return(ConvertUtils.ConvertResult.Success);
                }
                if (targetType == typeof(Version))
                {
                    Version version;
                    if (ConvertUtils.VersionTryParse(text, out version))
                    {
                        value = version;
                        return(ConvertUtils.ConvertResult.Success);
                    }
                    value = null;
                    return(ConvertUtils.ConvertResult.NoValidConversion);
                }
                else if (typeof(Type).IsAssignableFrom(targetType))
                {
                    value = Type.GetType(text, true);
                    return(ConvertUtils.ConvertResult.Success);
                }
            }
            if (targetType == typeof(System.Numerics.BigInteger))
            {
                value = ConvertUtils.ToBigInteger(initialValue);
                return(ConvertUtils.ConvertResult.Success);
            }
            if (initialValue is System.Numerics.BigInteger)
            {
                System.Numerics.BigInteger i = (System.Numerics.BigInteger)initialValue;
                value = ConvertUtils.FromBigInteger(i, targetType);
                return(ConvertUtils.ConvertResult.Success);
            }
            TypeConverter converter = TypeDescriptor.GetConverter(type);

            if (converter != null && converter.CanConvertTo(targetType))
            {
                value = converter.ConvertTo(null, culture, initialValue, targetType);
                return(ConvertUtils.ConvertResult.Success);
            }
            TypeConverter converter2 = TypeDescriptor.GetConverter(targetType);

            if (converter2 != null && converter2.CanConvertFrom(type))
            {
                value = converter2.ConvertFrom(null, culture, initialValue);
                return(ConvertUtils.ConvertResult.Success);
            }
            if (initialValue == DBNull.Value)
            {
                if (ReflectionUtils.IsNullable(targetType))
                {
                    value = ConvertUtils.EnsureTypeAssignable(null, type, targetType);
                    return(ConvertUtils.ConvertResult.Success);
                }
                value = null;
                return(ConvertUtils.ConvertResult.CannotConvertNull);
            }
            else
            {
                if (targetType.IsInterface() || targetType.IsGenericTypeDefinition() || targetType.IsAbstract())
                {
                    value = null;
                    return(ConvertUtils.ConvertResult.NotInstantiableType);
                }
                value = null;
                return(ConvertUtils.ConvertResult.NoValidConversion);
            }
        }
        public static bool CanConvertType(Type initialType, Type targetType, bool allowTypeNameToString)
        {
            ValidationUtils.ArgumentNotNull(initialType, "initialType");
            ValidationUtils.ArgumentNotNull(targetType, "targetType");
            if (ReflectionUtils.IsNullableType(targetType))
            {
                targetType = Nullable.GetUnderlyingType(targetType);
            }
            if (targetType == initialType)
            {
                return(true);
            }
            if (typeof(IConvertible).IsAssignableFrom(initialType) && typeof(IConvertible).IsAssignableFrom(targetType))
            {
                return(true);
            }
            if (initialType == typeof(DateTime) && targetType == typeof(DateTimeOffset))
            {
                return(true);
            }
            if (initialType == typeof(Guid) && (targetType == typeof(Guid) || targetType == typeof(string)))
            {
                return(true);
            }
            if (initialType == typeof(Type) && targetType == typeof(string))
            {
                return(true);
            }
            TypeConverter converter = ConvertUtils.GetConverter(initialType);

            if (converter != null && !ConvertUtils.IsComponentConverter(converter) && converter.CanConvertTo(targetType) && (allowTypeNameToString || converter.GetType() != typeof(TypeConverter)))
            {
                return(true);
            }
            TypeConverter converter2 = ConvertUtils.GetConverter(targetType);

            return((converter2 != null && !ConvertUtils.IsComponentConverter(converter2) && converter2.CanConvertFrom(initialType)) || (initialType == typeof(DBNull) && ReflectionUtils.IsNullable(targetType)));
        }
        public static object Convert(object initialValue, CultureInfo culture, Type targetType)
        {
            if (initialValue == null)
            {
                throw new ArgumentNullException("initialValue");
            }
            if (ReflectionUtils.IsNullableType(targetType))
            {
                targetType = Nullable.GetUnderlyingType(targetType);
            }
            Type type = initialValue.GetType();

            if (targetType == type)
            {
                return(initialValue);
            }
            if (initialValue is string && typeof(Type).IsAssignableFrom(targetType))
            {
                return(Type.GetType((string)initialValue, true));
            }
            if (targetType.get_IsInterface() || targetType.get_IsGenericTypeDefinition() || targetType.get_IsAbstract())
            {
                throw new ArgumentException("Target type {0} is not a value type or a non-abstract class.".FormatWith(CultureInfo.get_InvariantCulture(), new object[]
                {
                    targetType
                }), "targetType");
            }
            if (initialValue is IConvertible && typeof(IConvertible).IsAssignableFrom(targetType))
            {
                if (targetType.get_IsEnum())
                {
                    if (initialValue is string)
                    {
                        return(Enum.Parse(targetType, initialValue.ToString(), true));
                    }
                    if (ConvertUtils.IsInteger(initialValue))
                    {
                        return(Enum.ToObject(targetType, initialValue));
                    }
                }
                return(System.Convert.ChangeType(initialValue, targetType, culture));
            }
            if (initialValue is DateTime && targetType == typeof(DateTimeOffset))
            {
                return(new DateTimeOffset((DateTime)initialValue));
            }
            if (initialValue is string)
            {
                if (targetType == typeof(Guid))
                {
                    return(new Guid((string)initialValue));
                }
                if (targetType == typeof(Uri))
                {
                    return(new Uri((string)initialValue));
                }
                if (targetType == typeof(TimeSpan))
                {
                    return(TimeSpan.Parse((string)initialValue));
                }
            }
            TypeConverter converter = ConvertUtils.GetConverter(type);

            if (converter != null && converter.CanConvertTo(targetType))
            {
                return(converter.ConvertTo(null, culture, initialValue, targetType));
            }
            TypeConverter converter2 = ConvertUtils.GetConverter(targetType);

            if (converter2 != null && converter2.CanConvertFrom(type))
            {
                return(converter2.ConvertFrom(null, culture, initialValue));
            }
            if (initialValue != DBNull.Value)
            {
                throw new Exception("Can not convert from {0} to {1}.".FormatWith(CultureInfo.get_InvariantCulture(), new object[]
                {
                    type,
                    targetType
                }));
            }
            if (ReflectionUtils.IsNullable(targetType))
            {
                return(ConvertUtils.EnsureTypeAssignable(null, type, targetType));
            }
            throw new Exception("Can not convert null {0} into non-nullable {1}.".FormatWith(CultureInfo.get_InvariantCulture(), new object[]
            {
                type,
                targetType
            }));
        }
示例#11
0
        private static ConvertUtils.ConvertResult TryConvertInternal(object initialValue, CultureInfo culture, Type targetType, out object value)
        {
            Version version;

            if (initialValue == null)
            {
                throw new ArgumentNullException("initialValue");
            }
            if (ReflectionUtils.IsNullableType(targetType))
            {
                targetType = Nullable.GetUnderlyingType(targetType);
            }
            Type type = initialValue.GetType();

            if (targetType == type)
            {
                value = initialValue;
                return(ConvertUtils.ConvertResult.Success);
            }
            if (ConvertUtils.IsConvertible(initialValue.GetType()) && ConvertUtils.IsConvertible(targetType))
            {
                if (targetType.IsEnum())
                {
                    if (initialValue is string)
                    {
                        value = Enum.Parse(targetType, initialValue.ToString(), true);
                        return(ConvertUtils.ConvertResult.Success);
                    }
                    if (ConvertUtils.IsInteger(initialValue))
                    {
                        value = Enum.ToObject(targetType, initialValue);
                        return(ConvertUtils.ConvertResult.Success);
                    }
                }
                value = Convert.ChangeType(initialValue, targetType, culture);
                return(ConvertUtils.ConvertResult.Success);
            }
            if (initialValue is DateTime && targetType == typeof(DateTimeOffset))
            {
                value = new DateTimeOffset((DateTime)initialValue);
                return(ConvertUtils.ConvertResult.Success);
            }
            if (initialValue is byte[] && targetType == typeof(Guid))
            {
                value = new Guid((byte[])initialValue);
                return(ConvertUtils.ConvertResult.Success);
            }
            if (initialValue is Guid && targetType == typeof(byte[]))
            {
                value = ((Guid)initialValue).ToByteArray();
                return(ConvertUtils.ConvertResult.Success);
            }
            string str = initialValue as string;

            if (str != null)
            {
                if (targetType == typeof(Guid))
                {
                    value = new Guid(str);
                    return(ConvertUtils.ConvertResult.Success);
                }
                if (targetType == typeof(Uri))
                {
                    value = new Uri(str, UriKind.RelativeOrAbsolute);
                    return(ConvertUtils.ConvertResult.Success);
                }
                if (targetType == typeof(TimeSpan))
                {
                    value = ConvertUtils.ParseTimeSpan(str);
                    return(ConvertUtils.ConvertResult.Success);
                }
                if (targetType == typeof(byte[]))
                {
                    value = Convert.FromBase64String(str);
                    return(ConvertUtils.ConvertResult.Success);
                }
                if (targetType == typeof(Version))
                {
                    if (ConvertUtils.VersionTryParse(str, out version))
                    {
                        value = version;
                        return(ConvertUtils.ConvertResult.Success);
                    }
                    value = null;
                    return(ConvertUtils.ConvertResult.NoValidConversion);
                }
                if (typeof(Type).IsAssignableFrom(targetType))
                {
                    value = Type.GetType(str, true);
                    return(ConvertUtils.ConvertResult.Success);
                }
            }
            TypeConverter converter = ConvertUtils.GetConverter(type);

            if (converter != null && converter.CanConvertTo(targetType))
            {
                value = converter.ConvertTo(null, culture, initialValue, targetType);
                return(ConvertUtils.ConvertResult.Success);
            }
            TypeConverter typeConverter = ConvertUtils.GetConverter(targetType);

            if (typeConverter != null && typeConverter.CanConvertFrom(type))
            {
                value = typeConverter.ConvertFrom(null, culture, initialValue);
                return(ConvertUtils.ConvertResult.Success);
            }
            if (initialValue == DBNull.Value)
            {
                if (!ReflectionUtils.IsNullable(targetType))
                {
                    value = null;
                    return(ConvertUtils.ConvertResult.CannotConvertNull);
                }
                value = ConvertUtils.EnsureTypeAssignable(null, type, targetType);
                return(ConvertUtils.ConvertResult.Success);
            }
            if (!targetType.IsInterface() && !targetType.IsGenericTypeDefinition() && !targetType.IsAbstract())
            {
                value = null;
                return(ConvertUtils.ConvertResult.NoValidConversion);
            }
            value = null;
            return(ConvertUtils.ConvertResult.NotInstantiableType);
        }
示例#12
0
        public static object Convert(object initialValue, CultureInfo culture, Type targetType)
        {
            if (initialValue == null)
            {
                throw new ArgumentNullException("initialValue");
            }
            if (ReflectionUtils.IsNullableType(targetType))
            {
                targetType = Nullable.GetUnderlyingType(targetType);
            }
            Type type = initialValue.GetType();

            if (targetType == type)
            {
                return(initialValue);
            }
            if (ConvertUtils.IsConvertible(initialValue.GetType()) && ConvertUtils.IsConvertible(targetType))
            {
                if (targetType.IsEnum())
                {
                    if (initialValue is string)
                    {
                        return(Enum.Parse(targetType, initialValue.ToString(), true));
                    }
                    if (ConvertUtils.IsInteger(initialValue))
                    {
                        return(Enum.ToObject(targetType, initialValue));
                    }
                }
                return(System.Convert.ChangeType(initialValue, targetType, culture));
            }
            if (initialValue is DateTime && targetType == typeof(DateTimeOffset))
            {
                return(new DateTimeOffset((DateTime)initialValue));
            }
            if (initialValue is byte[] && targetType == typeof(Guid))
            {
                return(new Guid((byte[])initialValue));
            }
            if (initialValue is string)
            {
                if (targetType == typeof(Guid))
                {
                    return(new Guid((string)initialValue));
                }
                if (targetType == typeof(Uri))
                {
                    return(new Uri((string)initialValue, UriKind.RelativeOrAbsolute));
                }
                if (targetType == typeof(TimeSpan))
                {
                    return(ConvertUtils.ParseTimeSpan((string)initialValue));
                }
                if (targetType == typeof(byte[]))
                {
                    return(System.Convert.FromBase64String((string)initialValue));
                }
                if (typeof(Type).IsAssignableFrom(targetType))
                {
                    return(Type.GetType((string)initialValue, true));
                }
            }
            if (targetType == typeof(BigInteger))
            {
                return(ConvertUtils.ToBigInteger(initialValue));
            }
            if (initialValue is BigInteger)
            {
                return(ConvertUtils.FromBigInteger((BigInteger)initialValue, targetType));
            }
            TypeConverter converter = ConvertUtils.GetConverter(type);

            if (converter != null && converter.CanConvertTo(targetType))
            {
                return(converter.ConvertTo(null, culture, initialValue, targetType));
            }
            TypeConverter converter2 = ConvertUtils.GetConverter(targetType);

            if (converter2 != null && converter2.CanConvertFrom(type))
            {
                return(converter2.ConvertFrom(null, culture, initialValue));
            }
            if (initialValue == DBNull.Value)
            {
                if (ReflectionUtils.IsNullable(targetType))
                {
                    return(ConvertUtils.EnsureTypeAssignable(null, type, targetType));
                }
                throw new Exception("Can not convert null {0} into non-nullable {1}.".FormatWith(CultureInfo.InvariantCulture, type, targetType));
            }
            else
            {
                if (initialValue is INullable)
                {
                    return(ConvertUtils.EnsureTypeAssignable(ConvertUtils.ToValue((INullable)initialValue), type, targetType));
                }
                if (targetType.IsInterface() || targetType.IsGenericTypeDefinition() || targetType.IsAbstract())
                {
                    throw new ArgumentException("Target type {0} is not a value type or a non-abstract class.".FormatWith(CultureInfo.InvariantCulture, targetType), "targetType");
                }
                throw new InvalidOperationException("Can not convert from {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, type, targetType));
            }
        }