Table of Contents

XmlConverterDecoratorExtensions Class

Definition

Extension methods for the XmlConverter class hidden behind the IDecorator<T> interface.

public static class XmlConverterDecoratorExtensions
Inheritance
XmlConverterDecoratorExtensions

Examples

The following example demonstrates how to register and query XML converters with XmlConverterDecoratorExtensions.

using System;
using System.Collections.Generic;
using Cuemon;
using Cuemon.Diagnostics;
using Cuemon.Xml.Serialization.Converters;

namespace MyApp.Examples;

public static class XmlConverterDecoratorExtensionsExample
{
    public static void Demonstrate()
    {
        var converters = new List<XmlConverter>();
        var decorator = Decorator.Enclose((IList<XmlConverter>)converters);

        decorator.AddDateTimeConverter();
        decorator.AddTimeSpanConverter();
        decorator.AddStringConverter();
        decorator.AddUriConverter();
        decorator.AddEnumerableConverter();
        decorator.AddExceptionConverter(false, false);
        decorator.AddFailureConverter();
        decorator.AddExceptionDescriptorConverter(options => options.SensitivityDetails = FaultSensitivityDetails.All);
        decorator.AddXmlConverter<Version>(
            (writer, version, qe) =>
            {
                writer.WriteStartElement(qe?.LocalName ?? "Version");
                writer.WriteString(version.ToString());
                writer.WriteEndElement();
            },
            (reader, type) => Version.Parse(reader.ReadElementContentAsString())
        );
        decorator.InsertXmlConverter<Uri>(0);

        var writerConverter = decorator.FirstOrDefaultWriterConverter(typeof(Version));
        var readerConverter = decorator.FirstOrDefaultReaderConverter(typeof(Uri));

        Console.WriteLine(writerConverter != null);
        Console.WriteLine(readerConverter != null);
        Console.WriteLine(converters.Count);
    }
}

Methods

Name Description
AddDateTimeConverter(IDecorator<IList<XmlConverter>>)

Adds an DateTime XML converter to the enclosed IList<T> of the specified decorator.

AddEnumerableConverter(IDecorator<IList<XmlConverter>>, bool)

Adds an IEnumerable XML converter to the enclosed IList<T> of the specified decorator.

AddExceptionConverter(IDecorator<IList<XmlConverter>>, bool, bool)

Adds an Exception XML converter to the enclosed IList<T> of the specified decorator.

AddExceptionDescriptorConverter(IDecorator<IList<XmlConverter>>, Action<ExceptionDescriptorOptions>)

Adds an ExceptionDescriptor XML converter to the enclosed IList<T> of the specified decorator.

AddFailureConverter(IDecorator<IList<XmlConverter>>)

Adds an Failure XML converter to the enclosed IList<T> of the specified decorator.

AddStringConverter(IDecorator<IList<XmlConverter>>)

Adds an string XML converter to the enclosed IList<T> of the specified decorator.

AddTimeSpanConverter(IDecorator<IList<XmlConverter>>)

Adds an TimeSpan XML converter to the enclosed IList<T> of the specified decorator.

AddUriConverter(IDecorator<IList<XmlConverter>>)

Adds a Uri XML converter to the enclosed IList<T> of the specified decorator.

AddXmlConverter<T>(IDecorator<IList<XmlConverter>>, Action<XmlWriter, T, XmlQualifiedEntity>, Func<XmlReader, Type, T>, Func<Type, bool>, XmlQualifiedEntity)

Adds an XML converter to the enclosed IList<T> of the specified decorator.

FirstOrDefaultReaderConverter(IDecorator<IList<XmlConverter>>, Type)

Returns the first XmlConverter of the enclosed IList<T> of the specified decorator that CanConvert(Type) and CanRead the specified objectType; otherwise null if no XmlConverter is found.

FirstOrDefaultWriterConverter(IDecorator<IList<XmlConverter>>, Type)

Returns the first XmlConverter of the enclosed IList<T> of the specified decorator that CanConvert(Type) and CanWrite the specified objectType; otherwise null if no XmlConverter is found.

InsertXmlConverter<T>(IDecorator<IList<XmlConverter>>, int, Action<XmlWriter, T, XmlQualifiedEntity>, Func<XmlReader, Type, T>, Func<Type, bool>, XmlQualifiedEntity)

Inserts an XML converter to the enclosed IList<T> of the specified decorator at the specified index.

See Also