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> GetValueTypeFormatterObject <T>()
            where T : struct
        {
            FormatFunc <T>         formatFunc         = GetDefaultValueTypeFormatFunc <T>();
            FormatSpecificFunc <T> formatSpecificFunc = GetDefaultValueTypeFormatSpecificFunc <T>();

            return(ApplyOptionsAndGetValueTypeFormatterObject(formatFunc, formatSpecificFunc, _missingFormatSpecific));
        }
        private IFormatter <T> GetReferenceTypeFormatterObject <T>()
            where T : class
        {
            if (typeof(T) == typeof(string))
            {
                return((IFormatter <T>) new StringFormatter());
            }

            FormatFunc <T>         formatFunc         = GetDefaultReferenceTypeFormatFunc <T>();
            FormatSpecificFunc <T> formatSpecificFunc = GetDefaultReferenceTypeFormatSpecificFunc <T>();

            return(ApplyOptionsAndGetReferenceTypeFormatterObject(formatFunc, formatSpecificFunc, _missingFormatSpecific));
        }
Example #5
0
 /// <summary>
 /// Configures formatting of <typeparamref name="T"/> to use the provided functions.
 /// </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="formatSpecificFunc">A function that implements formatting functionality with a format specifier for <typeparamref name="T"/>.</param>
 public abstract void UseFunc <T>(FormatFunc <T> formatFunc, FormatSpecificFunc <T> formatSpecificFunc);
 public FunctorFormatterObject(FormatFunc <T> formatFunc, FormatSpecificFunc <T> formatSpecificFunc)
 {
     _formatFunc         = formatFunc;
     _formatSpecificFunc = formatSpecificFunc;
 }
        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));
        }
Example #8
0
 public ValueTypeFunctorFormatterObject(FormatFunc <T> formatFunc, FormatSpecificFunc <T> formatSpecificFunc)
     : base(formatFunc, formatSpecificFunc)
 {
 }