Пример #1
0
 private static bool EqualsFormattedNullValue(
     object value,
     object formattedNullValue,
     IFormatProvider formatInfo)
 {
     if (formattedNullValue is string && value is string)
     {
         return(string.Compare((string)value, (string)formattedNullValue, true, Formatter.GetFormatterCulture(formatInfo)) == 0);
     }
     return(object.Equals(value, formattedNullValue));
 }
Пример #2
0
        private static object FormatObjectInternal(
            object value,
            System.Type targetType,
            TypeConverter sourceConverter,
            TypeConverter targetConverter,
            string formatString,
            IFormatProvider formatInfo,
            object formattedNullValue)
        {
            if (value == DBNull.Value || value == null)
            {
                if (formattedNullValue != null)
                {
                    return(formattedNullValue);
                }
                if ((object)targetType == (object)Formatter.stringType)
                {
                    return((object)string.Empty);
                }
                if ((object)targetType == (object)Formatter.checkStateType)
                {
                    return((object)CheckState.Indeterminate);
                }
                return((object)null);
            }
            if ((object)targetType == (object)Formatter.stringType && value is IFormattable && !string.IsNullOrEmpty(formatString))
            {
                return((object)(value as IFormattable).ToString(formatString, formatInfo));
            }
            System.Type   type       = value.GetType();
            TypeConverter converter1 = TypeDescriptor.GetConverter(type);

            if (sourceConverter != null && sourceConverter != converter1 && sourceConverter.CanConvertTo(targetType))
            {
                return(sourceConverter.ConvertTo((ITypeDescriptorContext)null, Formatter.GetFormatterCulture(formatInfo), value, targetType));
            }
            TypeConverter converter2 = TypeDescriptor.GetConverter(targetType);

            if (targetConverter != null && targetConverter != converter2 && targetConverter.CanConvertFrom(type))
            {
                return(targetConverter.ConvertFrom((ITypeDescriptorContext)null, Formatter.GetFormatterCulture(formatInfo), value));
            }
            if ((object)targetType == (object)Formatter.checkStateType)
            {
                if ((object)type == (object)Formatter.booleanType)
                {
                    return((object)(CheckState)((bool)value ? 1 : 0));
                }
                if (sourceConverter == null)
                {
                    sourceConverter = converter1;
                }
                if (sourceConverter != null && sourceConverter.CanConvertTo(Formatter.booleanType))
                {
                    return((object)(CheckState)((bool)sourceConverter.ConvertTo((ITypeDescriptorContext)null, Formatter.GetFormatterCulture(formatInfo), value, Formatter.booleanType) ? 1 : 0));
                }
            }
            if (targetType.IsAssignableFrom(type))
            {
                return(value);
            }
            if (sourceConverter == null)
            {
                sourceConverter = converter1;
            }
            if (targetConverter == null)
            {
                targetConverter = converter2;
            }
            if (sourceConverter != null && sourceConverter.CanConvertTo(targetType))
            {
                return(sourceConverter.ConvertTo((ITypeDescriptorContext)null, Formatter.GetFormatterCulture(formatInfo), value, targetType));
            }
            if (targetConverter != null && targetConverter.CanConvertFrom(type))
            {
                return(targetConverter.ConvertFrom((ITypeDescriptorContext)null, Formatter.GetFormatterCulture(formatInfo), value));
            }
            if (!(value is IConvertible))
            {
                throw new FormatException(Formatter.GetCantConvertMessage(value, targetType));
            }
            return(Formatter.ChangeType(value, targetType, formatInfo));
        }
Пример #3
0
        private static object ParseObjectInternal(
            object value,
            System.Type targetType,
            System.Type sourceType,
            TypeConverter targetConverter,
            TypeConverter sourceConverter,
            IFormatProvider formatInfo,
            object formattedNullValue)
        {
            if (Formatter.EqualsFormattedNullValue(value, formattedNullValue, formatInfo) || value == DBNull.Value)
            {
                return((object)DBNull.Value);
            }
            TypeConverter converter1 = TypeDescriptor.GetConverter(targetType);

            if (targetConverter != null && converter1 != targetConverter && targetConverter.CanConvertFrom(sourceType))
            {
                return(targetConverter.ConvertFrom((ITypeDescriptorContext)null, Formatter.GetFormatterCulture(formatInfo), value));
            }
            TypeConverter converter2 = TypeDescriptor.GetConverter(sourceType);

            if (sourceConverter != null && converter2 != sourceConverter && sourceConverter.CanConvertTo(targetType))
            {
                return(sourceConverter.ConvertTo((ITypeDescriptorContext)null, Formatter.GetFormatterCulture(formatInfo), value, targetType));
            }
            if (value is string)
            {
                object method = Formatter.InvokeStringParseMethod(value, targetType, formatInfo);
                if (method != Formatter.parseMethodNotFound)
                {
                    return(method);
                }
            }
            else if (value is CheckState)
            {
                CheckState checkState = (CheckState)value;
                if (checkState == CheckState.Indeterminate)
                {
                    return((object)DBNull.Value);
                }
                if ((object)targetType == (object)Formatter.booleanType)
                {
                    return((object)(checkState == CheckState.Checked));
                }
                if (targetConverter == null)
                {
                    targetConverter = converter1;
                }
                if (targetConverter != null && targetConverter.CanConvertFrom(Formatter.booleanType))
                {
                    return(targetConverter.ConvertFrom((ITypeDescriptorContext)null, Formatter.GetFormatterCulture(formatInfo), (object)(checkState == CheckState.Checked)));
                }
            }
            else if (value != null && targetType.IsAssignableFrom(value.GetType()))
            {
                return(value);
            }
            if (targetConverter == null)
            {
                targetConverter = converter1;
            }
            if (sourceConverter == null)
            {
                sourceConverter = converter2;
            }
            if (targetConverter != null && targetConverter.CanConvertFrom(sourceType))
            {
                return(targetConverter.ConvertFrom((ITypeDescriptorContext)null, Formatter.GetFormatterCulture(formatInfo), value));
            }
            if (sourceConverter != null && sourceConverter.CanConvertTo(targetType))
            {
                return(sourceConverter.ConvertTo((ITypeDescriptorContext)null, Formatter.GetFormatterCulture(formatInfo), value, targetType));
            }
            if (!(value is IConvertible))
            {
                throw new FormatException(Formatter.GetCantConvertMessage(value, targetType));
            }
            return(Formatter.ChangeType(value, targetType, formatInfo));
        }