Skip to main content

Definition

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

Syntax

Summary

Provides metadata about a set of types that is relevant to XML serialization, and provides a base for source-generated contexts.

Usage

You don’t use XmlSerializerContext directly. You declare a partial class that derives from it, list your types with [XmlSerializable], and the source generator in the XmlEssentials package writes the rest of the class.
The generated half adds a static Default instance, a parameterless constructor, a constructor that takes XmlSerializerOptions, one XmlTypeInfo<T> property for each type, and an implementation of GetTypeInfo. Pass a type info property, or the context itself, to an XmlSerializer overload:
A context is bound to one XmlSerializerOptions instance, available from Options. The constructor makes the context the TypeInfoResolver of those options. The class is the counterpart of JsonSerializerContext in System.Text.Json. For the full procedure, see How to use source generation.

Remarks

This class is analogous to JsonSerializerContext in System.Text.Json.

Examples

Serialize and deserialize with generated type info

The code produces the following output:

Create a context from your own options

The code produces the following output:
View the complete sample on GitHub

Best Practices

  • Use Default unless you need other options. It’s created once and shared. When you do pass options to the constructor, create that context once and keep it.
  • Prefer the XmlTypeInfo<T> properties to the context overloads. OrderXmlContext.Default.Order fails to compile when the type isn’t in the context. XmlSerializer.Serialize(value, context) finds out at run time.
  • List every root type. A generated context uses no reflection: a type that it doesn’t list and can’t reach from a listed type is a NotSupportedException, unless it is a built-in value. That includes a collection or array type that you serialize as the document element, so list it too.
  • Declare the naming policies, DefaultPropertyMapping, and IncludeFields you serialize with in [XmlSourceGenerationOptions]. The write fast path is compiled for the values the attribute declares, and it’s used when the run-time options agree with them. With other options a context in the default mode writes from the metadata, with the result the reflection resolver gives.
  • Register a custom converter as you would without a context. A context in the default mode or in Metadata mode uses a converter in XmlSerializerOptions.Converters, and a converter named by [XmlConverter] on a type or a member.
  • List the base type of a hierarchy. The context reaches the types that [XmlDerivedType] names, on a type or on a member, and reads and writes them as the reflection resolver does.
  • Keep Serialization mode for types you only write. Type info generated in that mode has no metadata. It can’t read, and it writes only with the options the context was compiled for.

Considerations

  • The constructor changes the options you pass. It sets TypeInfoResolver on them, and throws InvalidOperationException when they’re read-only. XmlSerializerOptions.Default, Web, Strict, and Bcl are read-only. If you pass the same options to two contexts, the second one becomes the resolver.
  • A type info property belongs to the context’s options. OrderXmlContext.Default.Order.Options is the options of Default. Other options that ask the context for a type get type info of their own.
  • Contexts combine through TypeInfoResolverChain. A generated GetTypeInfo returns null for a type the context neither lists nor reaches, unless the type is a built-in value, an XML value, object, or a type with a custom converter, so the next context in the chain is asked. The options that hold the chain decide how each type is written: a generated context creates type info for the options that ask. A context you write by hand answers with whatever its GetTypeInfo(Type) returns.
  • A generated context follows every option. In the default mode and in Metadata mode, the context gives the serializer metadata, and the engine that the reflection resolver uses reads and writes from it. Naming policies, DefaultPropertyMapping, IncludeFields, IgnoreReadOnlyProperties, IgnoreReadOnlyFields, DefaultIgnoreCondition, ReferenceHandler, custom converters, and the other settings of XmlSerializerOptions apply when the type info is created, with the result the reflection resolver gives.
  • The write fast path is used when the options allow it. In the default mode, the generator also writes a method that writes a type directly. The context uses it when the naming policies, DefaultPropertyMapping, and IncludeFields of the options equal what [XmlSourceGenerationOptions] declares, IgnoreReadOnlyProperties and IgnoreReadOnlyFields are false, there is no ReferenceHandler, and the options hold no custom converter for the type or for a simple type the method writes directly. With any other options the context writes from the metadata.
  • Some types have no write fast path. In the default mode they’re written from the metadata, and no diagnostic is reported: a type that declares derived types, a type with a member that declares derived types or whose type or item type does, a member with [XmlConverter], mixed content, extension data, a member the context can’t access, and two members with the same C# name in the type’s hierarchy.
  • Serialization mode writes and can’t read. Type info generated in that mode has the write fast path and no metadata. Every read, and a write with options the fast path wasn’t compiled for, throws InvalidOperationException.
  • No member is found through reflection. Getters and setters are generated delegates, and init-only members, required members, non-public members marked [XmlInclude], and a non-public constructor marked [XmlConstructor] are reached through [UnsafeAccessor]. Every mode is safe for trimming and Native AOT. Under Native AOT, call the XmlSerializer overloads that take an XmlTypeInfo<T>.
  • Interceptors route other calls to the context. With the package’s build file in effect, XmlSerializer.Serialize<T>(value) and XmlSerializer.Deserialize<T>(input) for a listed type are rewritten to use the context’s Default instance. A call that passes an XmlSerializerOptions argument is never rewritten. The trimming and Native AOT analyzers still warn on a rewritten call, because they look at the method your code names, so pass the type info or the context in an application you publish that way. See Interceptors.
  • Unlike JsonSerializerContext, the class has no GeneratedSerializerOptions member. The generated code is a constructor and properties.

Constructors

.ctor Inherited

Inherited from object

Syntax

Properties

Options

Gets the run-time specified options of the context.

Syntax

Property Value

Type: CloudNimble.Text.Xml.XmlSerializerOptions The options bound to the context. If no options were passed when the context was created, a new instance is bound and returned.

Remarks

The XmlSerializerOptions.TypeInfoResolver of the options is this context.

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 Abstract

When overridden in a derived class, gets metadata for the specified type.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlTypeInfo? The metadata for type, or null if the context has no metadata for the type.

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.XmlSerializableAttribute
  • CloudNimble.Text.Xml.Serialization.XmlSourceGenerationOptionsAttribute
  • CloudNimble.Text.Xml.Serialization.XmlSourceGenerationMode
  • CloudNimble.Text.Xml.Serialization.XmlTypeInfo
  • CloudNimble.Text.Xml.Serialization.IXmlTypeInfoResolver
  • CloudNimble.Text.Xml.Serialization.DefaultXmlTypeInfoResolver
  • CloudNimble.Text.Xml.XmlSerializer
  • CloudNimble.Text.Xml.XmlSerializerOptions