コード例 #1
0
        public virtual object Convert(ITypeDescriptorContext context, CultureInfo culture, Type sourceType, Type destinationType, object value)
        {
            ArgumentUtility.CheckNotNull("sourceType", sourceType);
            ArgumentUtility.CheckNotNull("destinationType", destinationType);

            bool isNullableDestinationType = NullableTypeUtility.IsNullableType(destinationType);

            if (value == DBNull.Value && isNullableDestinationType)
            {
                return(GetValueOrEmptyString(destinationType, null));
            }

            if (value == null && !isNullableDestinationType)
            {
                throw new NotSupportedException(string.Format("Cannot convert value 'null' to non-nullable type '{0}'.", destinationType));
            }
            else if (value != null && !sourceType.IsInstanceOfType(value))
            {
                throw ArgumentUtility.CreateArgumentTypeException("value", value.GetType(), sourceType);
            }

            if (AreUnderlyingTypesEqual(sourceType, destinationType))
            {
                return(GetValueOrEmptyString(destinationType, value));
            }

            TypeConverterResult typeConverterResult = GetTypeConverter(sourceType, destinationType);

            if (!typeConverterResult.Equals(TypeConverterResult.Empty))
            {
                switch (typeConverterResult.TypeConverterType)
                {
                case TypeConverterType.SourceTypeConverter:
                    return(typeConverterResult.TypeConverter.ConvertTo(context, culture, value, destinationType));

                default:
                    Assertion.IsTrue(typeConverterResult.TypeConverterType == TypeConverterType.DestinationTypeConverter);
                    return(typeConverterResult.TypeConverter.ConvertFrom(context, culture, value));
                }
            }

            throw new NotSupportedException(string.Format("Cannot convert value '{0}' to type '{1}'.", value, destinationType));
        }
コード例 #2
0
        public virtual bool CanConvert(Type sourceType, Type destinationType)
        {
            ArgumentUtility.CheckNotNull("sourceType", sourceType);
            ArgumentUtility.CheckNotNull("destinationType", destinationType);

            if (sourceType == typeof(DBNull))
            {
                return(NullableTypeUtility.IsNullableType(destinationType));
            }

            if (AreUnderlyingTypesEqual(destinationType, sourceType))
            {
                return(true);
            }

            TypeConverterResult typeConverterResult = GetTypeConverter(sourceType, destinationType);

            return(!typeConverterResult.Equals(TypeConverterResult.Empty));
        }
コード例 #3
0
        public static object CheckType([InvokerParameterName] string argumentName, [NoEnumeration] object actualValue,
                                       Type expectedType)
        {
            if (actualValue == null)
            {
                if (NullableTypeUtility.IsNullableType_NoArgumentCheck(expectedType))
                {
                    return(null);
                }
                throw CreateArgumentTypeException(argumentName, null, expectedType);
            }

            // ReSharper disable UseMethodIsInstanceOfType
            if (!expectedType.GetTypeInfo().IsAssignableFrom(actualValue.GetType().GetTypeInfo()))
            {
                throw CreateArgumentTypeException(argumentName, actualValue.GetType(), expectedType);
            }
            // ReSharper restore UseMethodIsInstanceOfType

            return(actualValue);
        }
コード例 #4
0
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            ArgumentUtility.CheckNotNull("sourceType", sourceType);

            return(NullableTypeUtility.IsNullableType(sourceType));
        }
コード例 #5
0
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            ArgumentUtility.CheckNotNull("destinationType", destinationType);

            return(NullableTypeUtility.IsNullableType(destinationType));
        }