Ejemplo n.º 1
0
 public static object ToValue(INullable nullableValue)
 {
     if (nullableValue == null)
     {
         return(null);
     }
     if (nullableValue is SqlInt32)
     {
         return(ConvertUtils.ToValue((SqlInt32)nullableValue));
     }
     if (nullableValue is SqlInt64)
     {
         return(ConvertUtils.ToValue((SqlInt64)nullableValue));
     }
     if (nullableValue is SqlBoolean)
     {
         return(ConvertUtils.ToValue((SqlBoolean)nullableValue));
     }
     if (nullableValue is SqlString)
     {
         return(ConvertUtils.ToValue((SqlString)nullableValue));
     }
     if (nullableValue is SqlDateTime)
     {
         return(ConvertUtils.ToValue((SqlDateTime)nullableValue));
     }
     throw new ArgumentException("Unsupported INullable type: {0}".FormatWith(CultureInfo.InvariantCulture, nullableValue.GetType()));
 }
Ejemplo n.º 2
0
 public static object ToValue(INullable nullableValue)
 {
     if (nullableValue == null)
     {
         return((object)null);
     }
     if (nullableValue is SqlInt32)
     {
         return(ConvertUtils.ToValue((INullable)(ValueType)nullableValue));
     }
     if (nullableValue is SqlInt64)
     {
         return(ConvertUtils.ToValue((INullable)(ValueType)nullableValue));
     }
     if (nullableValue is SqlBoolean)
     {
         return(ConvertUtils.ToValue((INullable)(ValueType)nullableValue));
     }
     if (nullableValue is SqlString)
     {
         return(ConvertUtils.ToValue((INullable)(ValueType)nullableValue));
     }
     if (nullableValue is SqlDateTime)
     {
         return(ConvertUtils.ToValue((INullable)(ValueType)nullableValue));
     }
     else
     {
         throw new ArgumentException(StringUtils.FormatWith("Unsupported INullable type: {0}", (IFormatProvider)CultureInfo.InvariantCulture, (object)nullableValue.GetType()));
     }
 }
Ejemplo n.º 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));
                }
            }
        }
Ejemplo n.º 4
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));
            }
        }