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 typeConverter = ConvertUtils.GetConverter(targetType);

            if (typeConverter != null && !ConvertUtils.IsComponentConverter(typeConverter) && typeConverter.CanConvertFrom(initialType))
            {
                return(true);
            }
            if (initialType == typeof(DBNull) && ReflectionUtils.IsNullable(targetType))
            {
                return(true);
            }
            return(false);
        }
        private static ConvertUtils.ConvertResult TryConvertInternal(
            object initialValue,
            CultureInfo culture,
            Type targetType,
            out object value)
        {
            if (initialValue == null)
            {
                throw new ArgumentNullException(nameof(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, (IFormatProvider)culture);
                return(ConvertUtils.ConvertResult.Success);
            }
            switch (initialValue)
            {
            case DateTime _ when targetType == typeof(DateTimeOffset):
                value = (object)new DateTimeOffset((DateTime)initialValue);
                return(ConvertUtils.ConvertResult.Success);

            case byte[] _ when targetType == typeof(Guid):
                value = (object)new Guid((byte[])initialValue);
                return(ConvertUtils.ConvertResult.Success);

            case Guid _ when targetType == typeof(byte[]):
                value = (object)((Guid)initialValue).ToByteArray();
                return(ConvertUtils.ConvertResult.Success);

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

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

            if (converter2 != null && converter2.CanConvertFrom(type))
            {
                value = converter2.ConvertFrom((ITypeDescriptorContext)null, culture, initialValue);
                return(ConvertUtils.ConvertResult.Success);
            }
            if (initialValue == DBNull.Value)
            {
                if (ReflectionUtils.IsNullable(targetType))
                {
                    value = ConvertUtils.EnsureTypeAssignable((object)null, type, targetType);
                    return(ConvertUtils.ConvertResult.Success);
                }
                value = (object)null;
                return(ConvertUtils.ConvertResult.CannotConvertNull);
            }
            if (targetType.IsInterface() || targetType.IsGenericTypeDefinition() || targetType.IsAbstract())
            {
                value = (object)null;
                return(ConvertUtils.ConvertResult.NotInstantiableType);
            }
            value = (object)null;
            return(ConvertUtils.ConvertResult.NoValidConversion);
        }
Beispiel #3
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));
                }
            }
        }
        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
            }));
        }
Beispiel #5
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);
        }
        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));
            }
        }