Beispiel #1
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);
        }
Beispiel #2
0
        // Token: 0x06000E7F RID: 3711 RVA: 0x00057198 File Offset: 0x00055398
        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.NoValidConversion);
                }
                value = null;
                return(ConvertUtils.ConvertResult.NotInstantiableType);
            }
        }
        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));
            }
        }