Example #1
0
        public static object FormatObject(
            object value,
            System.Type targetType,
            TypeConverter sourceConverter,
            TypeConverter targetConverter,
            string formatString,
            IFormatProvider formatInfo,
            object formattedNullValue,
            object dataSourceNullValue)
        {
            if (Formatter.IsNullData(value, dataSourceNullValue))
            {
                value = (object)DBNull.Value;
            }
            System.Type type = targetType;
            targetType      = Formatter.NullableUnwrap(targetType);
            sourceConverter = Formatter.NullableUnwrap(sourceConverter);
            targetConverter = Formatter.NullableUnwrap(targetConverter);
            bool   flag = (object)targetType != (object)type;
            object obj  = Formatter.FormatObjectInternal(value, targetType, sourceConverter, targetConverter, formatString, formatInfo, formattedNullValue);

            if (type.IsValueType && obj == null && !flag)
            {
                throw new FormatException(Formatter.GetCantConvertMessage(value, targetType));
            }
            return(obj);
        }