Skip to main content

Definition

Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml.Serialization Inheritance: CloudNimble.Text.Xml.Serialization.XmlConverter

Syntax

Summary

Supports converting several types by using a factory pattern.

Usage

A factory is a converter that doesn’t convert anything itself. It tells the serializer which types it covers, in CanConvert, and creates the converter for each one, in CreateConverter. Use it for a family of types that one XmlConverter<T> can’t serve: every enumeration, or every closed form of a generic type.
Register a factory as you would a converter: add an instance to XmlSerializerOptions.Converters, or name the factory type in [XmlConverter]. CreateConverter must return an XmlConverter<T> whose T is exactly typeToConvert. The class is the counterpart of JsonConverterFactory in System.Text.Json.

Remarks

Use a factory for a family of types, such as every enumeration or every closed form of a generic type.

Examples

Write every enumeration in kebab case

The factory in the usage section creates one of these converters for each enumeration:
With the factory registered, both enumeration members of OrderTracking use it:
The code produces the following output:
View the complete sample on GitHub

Considerations

  • The returned converter must match the type exactly. If CreateConverter returns null, the serializer throws InvalidOperationException. It also throws when the converter returned is for another type.
  • CreateConverter runs once for each type and options instance. The serializer keeps the converter it returns, so the work of creating it isn’t repeated.
  • Order in the Converters list matters. The first converter or factory whose CanConvert returns true is used. Put a converter for one specific type before a factory that also covers it.
  • Reflection in a factory isn’t safe for Native AOT. A factory that calls MakeGenericType or Activator.CreateInstance on a constructed type needs code the ahead-of-time compiler may not have produced. In an AOT application, construct the converters directly, for example in a switch on typeToConvert.
  • Source generation. A source-generated context in the default mode or in Metadata mode uses a factory as the reflection resolver does. A factory in XmlSerializerOptions.Converters applies to every type it covers: enumerations, classes, and structs.
  • A factory makes CanConvert broad, so keep it precise. A factory that returns true for types it can’t handle hides the built-in converter for those types.

Constructors

.ctor Inherited

Inherited from CloudNimble.Text.Xml.Serialization.XmlConverter
Limits derivation to XmlConverter1` and XmlConverterFactory.

Syntax

.ctor Inherited

Inherited from object

Syntax

Properties

CanPopulate Inherited Virtual

Inherited from CloudNimble.Text.Xml.Serialization.XmlConverter
Gets whether the converter can read an element into a value that already exists.

Syntax

Property Value

Type: bool true for the converters of objects and dictionaries that the library creates; otherwise, false. A converter the caller wrote creates its value, so a member it reads is replaced.

IsValueConverter Inherited Virtual

Inherited from CloudNimble.Text.Xml.Serialization.XmlConverter
Gets whether this is one of the built-in converters that read and write a value from its lexical form, which the serializer can call directly.

Syntax

Property Value

Type: bool

Methods

CanConvert Inherited Abstract

Inherited from CloudNimble.Text.Xml.Serialization.XmlConverter
When overridden in a derived class, determines whether the converter instance can convert the specified type.

Syntax

Parameters

Returns

Type: bool true if the instance can convert typeToConvert; otherwise, false.

CreateConverter Abstract

When overridden in a derived class, creates a converter for the specified type.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlConverter? A converter that derives from XmlConverter1 for exactly *typeToConvert*. The serializer throws an [InvalidOperationException](https://learn.microsoft.com/dotnet/api/system.invalidoperationexception) if the factory returns [null`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null).

CreateTypeInfo Inherited Abstract

Inherited from CloudNimble.Text.Xml.Serialization.XmlConverter
Creates type info that reads and writes typeToConvert with this converter.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlTypeInfo? The type info, or null when this converter cannot produce values of that exact type.

Equals Inherited Virtual

Inherited from object

Syntax

Parameters

Returns

Type: bool

Equals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

GetHashCode Inherited Virtual

Inherited from object

Syntax

Returns

Type: int

GetType Inherited

Inherited from object

Syntax

Returns

Type: System.Type

MemberwiseClone Inherited

Inherited from object

Syntax

Returns

Type: object

Populate Inherited Virtual

Inherited from CloudNimble.Text.Xml.Serialization.XmlConverter
Reads the element reader is on into existing.

Syntax

Parameters

Exceptions

ReferenceEquals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

Resolve Inherited Abstract

Inherited from CloudNimble.Text.Xml.Serialization.XmlConverter
Returns the converter that does the work for typeToConvert: this converter, or the one a factory creates.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlConverter The converter.

ToString Inherited Virtual

Inherited from object

Syntax

Returns

Type: string?
  • CloudNimble.Text.Xml.Serialization.XmlConverter
  • CloudNimble.Text.Xml.Serialization.XmlConverterAttribute
  • CloudNimble.Text.Xml.XmlSerializerOptions
  • CloudNimble.Text.Xml.XmlNamingPolicy