/// <summary> /// Configures formatting of <typeparamref name="T"/> to use the provided function. /// </summary> /// <typeparam name="T">The type for which to configure formatting.</typeparam> /// <param name="formatFunc">A function that implements formatting functionality for <typeparamref name="T"/>.</param> /// <param name="missingFormatSpecific">A value indicating how to configure format functionality if the type does not have a ToString overload that accepts a format specification.</param> public abstract void UseFunc <T>(FormatFunc <T> formatFunc, MissingFormatSpecificHandling missingFormatSpecific);
/// <summary> /// Configures formatting of <typeparamref name="T"/> to use its type converter. /// </summary> /// <typeparam name="T">The type for which to configure formatting.</typeparam> /// <param name="missingFormatSpecific">A value indicating how to configure format functionality if the type does not have a ToString overload that accepts a format specification.</param> public abstract void UseTypeConverter <T>(MissingFormatSpecificHandling missingFormatSpecific);
/// <summary> /// Configures formatting of <typeparamref name="T"/> to explicitly use the default implementation. /// </summary> /// <typeparam name="T">The type for which to configure formatting.</typeparam> /// <param name="missingFormatSpecific">A value indicating how to configure format functionality if the type does not have a ToString overload that accepts a format specification.</param> public abstract void UseDefault <T>(MissingFormatSpecificHandling missingFormatSpecific);
private static FormatSpecificFunc <T> CreateFormatSpecificFuncForTypeConverter <T>(TypeConverter converter, MissingFormatSpecificHandling missingFormatSpecific) => null;
private IFormatter <T> ApplyOptionsAndGetReferenceTypeFormatterObject <T>(FormatFunc <T> formatFunc, FormatSpecificFunc <T> formatSpecificFunc, MissingFormatSpecificHandling missingFormatSpecific) { FormatFunc <T> referenceTypeFormatFunc = formatFunc; FormatSpecificFunc <T> referenceTypeFormatSpecificFunc = formatSpecificFunc; // formatFunc will never be null because ToString() is always available. if (_referenceTypesFormatNullToNull) { referenceTypeFormatFunc = value => value != null?formatFunc(value) : null; } if (formatSpecificFunc is null) { if (missingFormatSpecific == MissingFormatSpecificHandling.UseToString) { referenceTypeFormatSpecificFunc = (value, format) => referenceTypeFormatFunc(value); } else if (missingFormatSpecific == MissingFormatSpecificHandling.ReturnNull) { referenceTypeFormatSpecificFunc = FormatSpecificReturnsNull; } else if (missingFormatSpecific == MissingFormatSpecificHandling.ReturnEmptyString) { referenceTypeFormatSpecificFunc = FormatSpecificReturnsEmptyString; } else { referenceTypeFormatSpecificFunc = FormatSpecificThrowsNotSupportedException; } } return(new FunctorFormatterObject <T>(referenceTypeFormatFunc, referenceTypeFormatSpecificFunc)); }
private IFormatter <T> ApplyOptionsAndGetValueTypeFormatterObject <T>(FormatFunc <T> formatFunc, FormatSpecificFunc <T> formatSpecificFunc, MissingFormatSpecificHandling missingFormatSpecific) { FormatFunc <T> valueTypeFormatFunc = formatFunc; FormatSpecificFunc <T> valueTypeFormatSpecificFunc = formatSpecificFunc; // formatFunc will never be null because ToString() is always available. if (formatSpecificFunc is null) { if (_missingFormatSpecific == MissingFormatSpecificHandling.ThrowNotSupportedException) { valueTypeFormatSpecificFunc = FormatSpecificThrowsNotSupportedException; } else if (_missingFormatSpecific == MissingFormatSpecificHandling.ReturnEmptyString) { valueTypeFormatSpecificFunc = FormatSpecificReturnsEmptyString; } else if (_missingFormatSpecific == MissingFormatSpecificHandling.ReturnNull) { valueTypeFormatSpecificFunc = FormatSpecificReturnsNull; } else { valueTypeFormatSpecificFunc = (value, format) => valueTypeFormatFunc(value); } } return(new ValueTypeFunctorFormatterObject <T>(valueTypeFormatFunc, valueTypeFormatSpecificFunc)); }
private IFormatter <T> AltCreateFormatterObject <T>(FormatFunc <T> formatFunc, FormatSpecificFunc <T> formatSpecificFunc, MissingFormatSpecificHandling missingFormatSpecific) { Type type = typeof(T); string methodName = type.GetTypeInfo().IsValueType ? nameof(ApplyOptionsAndGetValueTypeFormatterObject) : nameof(ApplyOptionsAndGetReferenceTypeFormatterObject); MethodInfo factoryMethod = ReflectionHelper.GetPrivateGenericMethod(typeof(FormatterContainer), methodName, type); Func <FormatFunc <T>, FormatSpecificFunc <T>, MissingFormatSpecificHandling, IFormatter <T> > factory = factoryMethod.CreateDelegate <Func <FormatFunc <T>, FormatSpecificFunc <T>, MissingFormatSpecificHandling, IFormatter <T> > >(this); return(factory(formatFunc, formatSpecificFunc, missingFormatSpecific)); }