Пример #1
0
        /// <summary>
        /// Returns the text passed across unless <paramref name="resolver"/> is supplied, in which case
        /// it is passed through the resolver.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="resolver"></param>
        /// <returns></returns>
        public static string FormatString(string text, TypeFormatterResolver resolver)
        {
            var formatter = resolver?.StringFormatter;

            return(formatter == null || text == null
                ? text
                : formatter.Format(text));
        }
Пример #2
0
        /// <summary>
        /// Returns text describing the byte.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="resolver"></param>
        /// <returns></returns>
        public static string FormatByte(byte?value, TypeFormatterResolver resolver)
        {
            var formatter = resolver?.ByteFormatter;

            return(formatter == null || value == null
                ? FormatByte(value)
                : formatter.Format(value.Value));
        }
Пример #3
0
        /// <summary>
        /// Returns text describing the byte array.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="resolver"></param>
        /// <returns></returns>
        public static string FormatByteArray(byte[] value, TypeFormatterResolver resolver)
        {
            var formatter = resolver?.ByteArrayFormatter;

            return(formatter == null
                ? _ByteArray_Mime64_Formatter.Format(value)
                : formatter.Format(value));
        }
Пример #4
0
        /// <summary>
        /// Returns text describing the decimal.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="resolver"></param>
        /// <returns></returns>
        public static string FormatDecimal(decimal?value, TypeFormatterResolver resolver)
        {
            var formatter = resolver?.DecimalFormatter;

            return(formatter == null || value == null
                ? FormatDecimal(value)
                : formatter.Format(value.Value));
        }
Пример #5
0
        /// <summary>
        /// Returns text describing the GUID.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="resolver"></param>
        /// <returns></returns>
        public static string FormatGuid(Guid?value, TypeFormatterResolver resolver)
        {
            var formatter = resolver?.GuidFormatter;

            return(formatter == null || value == null
                ? FormatGuid(value)
                : formatter.Format(value.Value));
        }
Пример #6
0
        /// <summary>
        /// Returns text describing the float.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="resolver"></param>
        /// <returns></returns>
        public static string FormatFloat(float?value, TypeFormatterResolver resolver)
        {
            var formatter = resolver?.FloatFormatter;

            return(formatter == null || value == null
                ? FormatFloat(value)
                : formatter.Format(value.Value));
        }
Пример #7
0
        /// <summary>
        /// Returns text describing the unsigned long int.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="resolver"></param>
        /// <returns></returns>
        public static string FormatUInt64(UInt64?value, TypeFormatterResolver resolver)
        {
            var formatter = resolver?.UInt64Formatter;

            return(formatter == null || value == null
                ? FormatUInt64(value)
                : formatter.Format(value.Value));
        }
Пример #8
0
        /// <summary>
        /// Returns text describing the int.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="resolver"></param>
        /// <returns></returns>
        public static string FormatInt32(Int32?value, TypeFormatterResolver resolver)
        {
            var formatter = resolver?.Int32Formatter;

            return(formatter == null || value == null
                ? FormatInt32(value)
                : formatter.Format(value.Value));
        }
Пример #9
0
        /// <summary>
        /// Returns text describing the char.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="resolver"></param>
        /// <returns></returns>
        public static string FormatChar(char?value, TypeFormatterResolver resolver)
        {
            var formatter = resolver?.CharFormatter;

            return(formatter == null || value == null
                ? FormatChar(value)
                : formatter.Format(value.Value));
        }
Пример #10
0
        /// <summary>
        /// Adds the resolver passed across to the cache. Must be called within a lock.
        /// </summary>
        /// <param name="resolver"></param>
        private static void AddToCache(TypeFormatterResolver resolver)
        {
            lock (_SyncLock) {
                var copy = new TypeFormatterResolver[_Resolvers.Length + 1];
                Array.Copy(_Resolvers, 0, copy, 0, _Resolvers.Length);
                copy[copy.Length - 1] = resolver;

                _Resolvers = copy;
            }
        }
Пример #11
0
 /// <summary>
 /// Returns text describing the DateTimeOffset.
 /// </summary>
 /// <param name="value"></param>
 /// <param name="resolver"></param>
 /// <returns></returns>
 public static string FormatDateTimeOffset(DateTimeOffset?value, TypeFormatterResolver resolver)
 {
     if (value == null)
     {
         return(null);
     }
     else
     {
         var formatter = resolver?.DateTimeOffsetFormatter;
         return(formatter == null
             ? _DateTimeOffset_Iso8601_Formatter.Format(value.Value)
             : formatter.Format(value.Value));
     }
 }
Пример #12
0
        /// <summary>
        /// Returns an existing <see cref="TypeFormatterResolver"/> that has the exact same formatters as
        /// those passed in or creates and returns a new resolver if no such formatter combination exists.
        /// </summary>
        /// <param name="formatters"></param>
        /// <returns></returns>
        public static TypeFormatterResolver Find(params ITypeFormatter[] formatters)
        {
            formatters = formatters ?? new ITypeFormatter[0];
            var result = FindInCache(formatters);

            if (result == null)
            {
                lock (_SyncLock) {
                    result = FindInCache(formatters);
                    if (result == null)
                    {
                        result = new TypeFormatterResolver(formatters);
                        AddToCache(result);
                    }
                }
            }

            return(result);
        }
Пример #13
0
        /// <summary>
        /// Returns text describing the <paramref name="value"/>.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="typeFormatterResolver"></param>
        /// <returns></returns>
        public static string FormatType(object value, TypeFormatterResolver typeFormatterResolver)
        {
            string result = null;

            if (value != null)
            {
                var type = value.GetType();
                if (type == typeof(string))
                {
                    result = typeFormatterResolver == null
                        ? FormatString((string)value)
                        : FormatString((string)value, typeFormatterResolver);
                }
                else if (value != null)
                {
                    if (type == typeof(bool) || type == typeof(bool?))
                    {
                        result = typeFormatterResolver == null
                            ? FormatBool((bool?)value)
                            : FormatBool((bool?)value, typeFormatterResolver);
                    }
                    else if (type == typeof(byte) || type == typeof(byte?))
                    {
                        result = typeFormatterResolver == null
                            ? FormatByte((byte?)value)
                            : FormatByte((byte?)value, typeFormatterResolver);
                    }
                    else if (type == typeof(char) || type == typeof(char?))
                    {
                        result = typeFormatterResolver == null
                            ? FormatChar((char?)value)
                            : FormatChar((char?)value, typeFormatterResolver);
                    }
                    else if (type == typeof(Int16) || type == typeof(Int16?))
                    {
                        result = typeFormatterResolver == null
                            ? FormatInt16((Int16?)value)
                            : FormatInt16((Int16?)value, typeFormatterResolver);
                    }
                    else if (type == typeof(UInt16) || type == typeof(UInt16?))
                    {
                        result = typeFormatterResolver == null
                            ? FormatUInt16((UInt16?)value)
                            : FormatUInt16((UInt16?)value, typeFormatterResolver);
                    }
                    else if (type == typeof(Int32) || type == typeof(Int32?))
                    {
                        result = typeFormatterResolver == null
                            ? FormatInt32((Int32?)value)
                            : FormatInt32((Int32?)value, typeFormatterResolver);
                    }
                    else if (type == typeof(UInt32) || type == typeof(UInt32?))
                    {
                        result = typeFormatterResolver == null
                            ? FormatUInt32((UInt32?)value)
                            : FormatUInt32((UInt32?)value, typeFormatterResolver);
                    }
                    else if (type == typeof(Int64) || type == typeof(Int64?))
                    {
                        result = typeFormatterResolver == null
                            ? FormatInt64((Int64?)value)
                            : FormatInt64((Int64?)value, typeFormatterResolver);
                    }
                    else if (type == typeof(UInt64) || type == typeof(UInt64?))
                    {
                        result = typeFormatterResolver == null
                            ? FormatUInt64((UInt64?)value)
                            : FormatUInt64((UInt64?)value, typeFormatterResolver);
                    }
                    else if (type == typeof(float) || type == typeof(float?))
                    {
                        result = typeFormatterResolver == null
                            ? FormatFloat((float?)value)
                            : FormatFloat((float?)value, typeFormatterResolver);
                    }
                    else if (type == typeof(double) || type == typeof(double?))
                    {
                        result = typeFormatterResolver == null
                            ? FormatDouble((double?)value)
                            : FormatDouble((double?)value, typeFormatterResolver);
                    }
                    else if (type == typeof(decimal) || type == typeof(decimal?))
                    {
                        result = typeFormatterResolver == null
                            ? FormatDecimal((decimal?)value)
                            : FormatDecimal((decimal?)value, typeFormatterResolver);
                    }
                    else if (type == typeof(DateTime) || type == typeof(DateTime?))
                    {
                        result = typeFormatterResolver == null
                            ? FormatDateTime((DateTime?)value)
                            : FormatDateTime((DateTime?)value, typeFormatterResolver);
                    }
                    else if (type == typeof(DateTimeOffset) || type == typeof(DateTimeOffset?))
                    {
                        result = typeFormatterResolver == null
                            ? FormatDateTimeOffset((DateTimeOffset?)value)
                            : FormatDateTimeOffset((DateTimeOffset?)value, typeFormatterResolver);
                    }
                    else if (type == typeof(Guid) || type == typeof(Guid?))
                    {
                        result = typeFormatterResolver == null
                            ? FormatGuid((Guid?)value)
                            : FormatGuid((Guid?)value, typeFormatterResolver);
                    }
                    else if (type == typeof(byte[]))
                    {
                        result = typeFormatterResolver == null
                            ? FormatByteArray((byte[])value)
                            : FormatByteArray((byte[])value, typeFormatterResolver);
                    }
                    else
                    {
                        result = value.ToString();
                    }
                }
            }

            return(result);
        }