Definition
Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml.SerializationSyntax
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 useXmlSerializerContext 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.
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:
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 toJsonSerializerContext in System.Text.Json.
Examples
Serialize and deserialize with generated type info
Create a context from your own options
Best Practices
- Use
Defaultunless 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.Orderfails 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, andIncludeFieldsyou 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
Metadatamode uses a converter inXmlSerializerOptions.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
Serializationmode 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
TypeInfoResolveron them, and throwsInvalidOperationExceptionwhen they’re read-only.XmlSerializerOptions.Default,Web,Strict, andBclare 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.Optionsis the options ofDefault. Other options that ask the context for a type get type info of their own. - Contexts combine through
TypeInfoResolverChain. A generatedGetTypeInforeturnsnullfor 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 itsGetTypeInfo(Type)returns. - A generated context follows every option. In the default mode and in
Metadatamode, 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 ofXmlSerializerOptionsapply 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, andIncludeFieldsof the options equal what[XmlSourceGenerationOptions]declares,IgnoreReadOnlyPropertiesandIgnoreReadOnlyFieldsarefalse, there is noReferenceHandler, 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. Serializationmode 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, throwsInvalidOperationException.- No member is found through reflection. Getters and setters are generated delegates, and
init-only members,requiredmembers, 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 theXmlSerializeroverloads that take anXmlTypeInfo<T>. - Interceptors route other calls to the context. With the package’s build file in effect,
XmlSerializer.Serialize<T>(value)andXmlSerializer.Deserialize<T>(input)for a listed type are rewritten to use the context’sDefaultinstance. A call that passes anXmlSerializerOptionsargument 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 noGeneratedSerializerOptionsmember. The generated code is a constructor and properties.
Constructors
.ctor Inherited
Inherited from
objectSyntax
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
objectSyntax
Parameters
Returns
Type:bool
Equals Inherited
Inherited from
objectSyntax
Parameters
Returns
Type:bool
GetHashCode Inherited Virtual
Inherited from
objectSyntax
Returns
Type:int
GetType Inherited
Inherited from
objectSyntax
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
objectSyntax
Returns
Type:object
ReferenceEquals Inherited
Inherited from
objectSyntax
Parameters
Returns
Type:bool
ToString Inherited Virtual
Inherited from
objectSyntax
Returns
Type:string?
Related APIs
- 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