Пример #1
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);
            }
            else if (!typeof(T).CanBeInstantiated())
            {
                Formatter.RegisterLazilyForConcreteTypesOf(
                    typeof(T),
                    (o, writer) => formatter((T)o, writer), mimeType);
                return;
            }

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