Definition
Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml.Serialization Inheritance: CloudNimble.Text.Xml.Serialization.XmlConverterSyntax
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, inCanConvert, 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.
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:OrderTracking use it:
Considerations
- The returned converter must match the type exactly. If
CreateConverterreturnsnull, the serializer throwsInvalidOperationException. It also throws when the converter returned is for another type. CreateConverterruns 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
Converterslist matters. The first converter or factory whoseCanConvertreturnstrueis 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
MakeGenericTypeorActivator.CreateInstanceon 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 aswitchontypeToConvert. - Source generation. A source-generated context in the default mode or in
Metadatamode uses a factory as the reflection resolver does. A factory inXmlSerializerOptions.Convertersapplies to every type it covers: enumerations, classes, and structs. - A factory makes
CanConvertbroad, so keep it precise. A factory that returnstruefor types it can’t handle hides the built-in converter for those types.
Constructors
.ctor Inherited
Inherited from
CloudNimble.Text.Xml.Serialization.XmlConverterXmlConverter1` and XmlConverterFactory.
Syntax
.ctor Inherited
Inherited from
objectSyntax
Properties
CanPopulate Inherited Virtual
Inherited from
CloudNimble.Text.Xml.Serialization.XmlConverterSyntax
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.XmlConverterSyntax
Property Value
Type:bool
Methods
CanConvert Inherited Abstract
Inherited from
CloudNimble.Text.Xml.Serialization.XmlConverterSyntax
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.XmlConverterSyntax
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
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
MemberwiseClone Inherited
Inherited from
objectSyntax
Returns
Type:object
Populate Inherited Virtual
Inherited from
CloudNimble.Text.Xml.Serialization.XmlConverterSyntax
Parameters
Exceptions
ReferenceEquals Inherited
Inherited from
objectSyntax
Parameters
Returns
Type:bool
Resolve Inherited Abstract
Inherited from
CloudNimble.Text.Xml.Serialization.XmlConverterSyntax
Parameters
Returns
Type:CloudNimble.Text.Xml.Serialization.XmlConverter
The converter.
ToString Inherited Virtual
Inherited from
objectSyntax
Returns
Type:string?
Related APIs
- CloudNimble.Text.Xml.Serialization.XmlConverter
- CloudNimble.Text.Xml.Serialization.XmlConverterAttribute
- CloudNimble.Text.Xml.XmlSerializerOptions
- CloudNimble.Text.Xml.XmlNamingPolicy