コード例 #1
0
        /// <summary>
        /// Registers a formatter to be used when formatting instances of type <typeparamref name="T" />.
        /// </summary>
        public static void Register(Action <T, TextWriter> formatter)
        {
            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }

            if (typeof(T) == typeof(Type))
            {
                // special treatment is needed since typeof(Type) == System.RuntimeType, which is not public
                // ReSharper disable once PossibleMistakenCallToGetType.2
                Formatter.Register(typeof(Type).GetType(), (o, writer) => formatter((T)o, writer));
            }

            Custom = formatter;
        }
コード例 #2
0
        public static void Register(
            Action <T, TextWriter> formatter,
            string mimeType = PlainTextFormatter.MimeType)
        {
            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }

            if (typeof(T) == typeof(Type))
            {
                // special treatment is needed since typeof(Type) == System.RuntimeType, which is not public
                // ReSharper disable once PossibleMistakenCallToGetType.2
                Formatter.Register(
                    typeof(Type).GetType(),
                    (o, writer) => formatter((T)o, writer),
                    mimeType);
            }

            Formatter.TypeFormatters[(typeof(T), mimeType)] = new AnonymousTypeFormatter <T>(formatter, mimeType);