Skip to main content

Definition

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

Syntax

Summary

Defines the default, reflection-based XML contract resolver used by XmlSerializer. This class cannot be inherited.

Usage

DefaultXmlTypeInfoResolver is the resolver behind every XmlSerializer call that passes only a value, or a value and options. It finds the converter for a type and wraps it in type info. For a class or struct, that converter is built by inspecting the type’s members and attributes with reflection. You rarely name the class. An XmlSerializerOptions instance with no TypeInfoResolver is given the shared DefaultXmlTypeInfoResolver.Instance the first time an XmlSerializer method that takes options uses it:
The code produces the following output:
The resolver looks for a converter in this order: the Converters list of the options, then [XmlConverter] on the type, then the built-in converters. It returns null when none of them handles the type. The class has no public constructor. Use Instance. It’s the counterpart of DefaultJsonTypeInfoResolver in System.Text.Json. View the complete sample on GitHub

Remarks

The resolver builds type info from the converters of an XmlSerializerOptions instance: its XmlSerializerOptions.Converters, the XmlConverterAttribute on the type, and the built-in converters.

Considerations

  • It isn’t safe for trimming or Native AOT, and it says so. The resolver reads members with reflection and creates generic converter types at run time, so the class carries RequiresUnreferencedCode and RequiresDynamicCode: the compiler warns wherever a trimmed or Native AOT application names it. In such an application, use a source-generated context. See Publish with Native AOT.
  • Options get it only from members that need reflection. An options instance with no resolver is given this one by an XmlSerializer method that takes options, by XmlSerializerOptions.GetConverter, by MakeReadOnly(populateMissingResolver: true), and by the shared Default, Web, Strict, and Bcl instances. Reading TypeInfoResolver or calling GetTypeInfo never brings it in.
  • The first request for a type is the expensive one. It reflects over the type’s members and builds its converter, which costs many times what a later call costs. Create one XmlSerializerOptions instance and reuse it, because the options cache what the resolver returns.
  • There are no modifiers. DefaultJsonTypeInfoResolver in System.Text.Json lets you add modifiers that edit a contract. This class has none. Change a mapping with attributes, options, or a custom converter.
  • Generated contexts don’t use it. A source-generated context builds the type info of every type it lists from generated code, and takes a built-in value, an XML value, object, and a type you supplied a converter for from converters that exist already.
  • It’s stateless and shared. Instance is safe to use from any thread. The caching is done by the options, not by the resolver.

Constructors

.ctor Inherited

Inherited from object

Syntax

Properties

Instance

Gets the shared instance of the DefaultXmlTypeInfoResolver class.

Syntax

Property Value

Type: CloudNimble.Text.Xml.Serialization.DefaultXmlTypeInfoResolver The shared resolver.

Methods

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

GetTypeInfo

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlTypeInfo?

Exceptions

MemberwiseClone Inherited

Inherited from object

Syntax

Returns

Type: object

ReferenceEquals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

ToString Inherited Virtual

Inherited from object

Syntax

Returns

Type: string?
  • CloudNimble.Text.Xml.Serialization.IXmlTypeInfoResolver
  • CloudNimble.Text.Xml.Serialization.XmlSerializerContext
  • CloudNimble.Text.Xml.Serialization.XmlTypeInfo
  • CloudNimble.Text.Xml.Serialization.XmlConverter
  • CloudNimble.Text.Xml.Serialization.XmlConverterAttribute
  • CloudNimble.Text.Xml.XmlSerializerOptions
  • CloudNimble.Text.Xml.XmlSerializer