Definition
Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml.SerializationSyntax
Summary
Defines a resolver of the XML serialization contract for requested types.Usage
A resolver answers one question for the serializer: what is the contract for this type?XmlSerializerOptions.TypeInfoResolver holds the resolver an options instance uses. The library has two implementations, and most applications use only these:
DefaultXmlTypeInfoResolver, which builds contracts with reflection at run time.- Every source-generated
XmlSerializerContext, which builds type info from the metadata the generator wrote.
XmlSerializer call that takes the options gives them DefaultXmlTypeInfoResolver.Instance. The following example prints the resolver that built the contract for a type no context lists:
null for a type it doesn’t handle. XmlSerializerOptions.TypeInfoResolverChain is a list view of the resolver: the options ask each entry in order and use the first answer that isn’t null.
The interface is the counterpart of IJsonTypeInfoResolver in System.Text.Json.
View the complete sample on GitHub
Considerations
- A resolver is asked once for each type. The options cache the answer, and the first request makes the options read-only. Changing what a resolver returns later has no effect on options that already asked.
- The type info must be for the requested type. If a resolver returns type info for another type, the options throw
InvalidOperationException. XmlTypeInfohas no public constructor. A resolver you write returns type info it gets from another resolver or from a context, or creates it withXmlMetadataServices.CreateObjectInfofrom metadata it supplies, as a generated context does. Unlike System.Text.Json, there’s no modifier API for changing a contract.- A generated context answers for the options that ask. Its
XmlTypeInfo<T>properties are bound to its ownOptions. When other options ask it, as they do in aTypeInfoResolverChain, it creates type info for those options. It returnsnullfor a type it 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 resolver in the chain is asked. - There’s no resolver until one is set or given.
TypeInfoResolverisnullon a new instance, andGetTypeInfoon options that have none throwsInvalidOperationException. AnXmlSerializermethod that takes options gives them the reflection resolver on first use, and so doesMakeReadOnly(populateMissingResolver: true). WhenXmlSerializer.IsReflectionEnabledByDefaultisfalse, nothing gives it, and such a call throws.
Methods
GetTypeInfo Abstract
Resolves an XmlTypeInfo contract for the requested type and options.Syntax
Parameters
Returns
Type:CloudNimble.Text.Xml.Serialization.XmlTypeInfo?
The contract that matches type, or null if this resolver does not handle the type.
Related APIs
- CloudNimble.Text.Xml.Serialization.DefaultXmlTypeInfoResolver
- CloudNimble.Text.Xml.Serialization.XmlSerializerContext
- CloudNimble.Text.Xml.Serialization.XmlTypeInfo
- CloudNimble.Text.Xml.XmlSerializerOptions
- CloudNimble.Text.Xml.XmlSerializer