Skip to main content

Definition

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

Syntax

Summary

Specifies the converter type to use when placed on a property, field, or type. This class cannot be inherited.

Usage

Apply [XmlConverter] to a class, struct, or enumeration to name the converter the serializer uses for it. Every XmlSerializerOptions instance then uses that converter, with nothing to register. Apply it to a property or field instead to convert that one member.
The code produces the following output:
The converter type must derive from XmlConverter<T> or XmlConverterFactory and have a public parameterless constructor. The attribute is the counterpart of JsonConverterAttribute in System.Text.Json. View the complete sample on GitHub

Remarks

On a type, the converter reads and writes every value of the type, unless XmlSerializerOptions.Converters holds a converter for it. On a property or field, the converter reads and writes that member only, and comes before both. It handles the whole value of the member: a collection is not taken apart into items, the member may be an attribute or text, and a converter for T serves a member of type T?. A source-generated context applies the attribute on a type and on a member, as the reflection resolver does. The converter is a type argument, so the compiler checks that it is a converter and can be created, and no reflection is needed to create it.

Type Parameters

  • TConverter - The type of the converter. It derives from XmlConverter and has a public parameterless constructor.

Examples

Considerations

  • On a property or field, the converter serves that member only. It reads and writes the whole value of the member, as [JsonConverter] does in System.Text.Json: a collection isn’t taken apart into items, the member may be an attribute or text, and a converter for T serves a member of type T?. A member attribute comes before XmlSerializerOptions.Converters and before an attribute on the type.
  • A converter in the options wins over the attribute on a type. XmlSerializerOptions.Converters is searched before the attribute on the type, so a caller can override the converter a type names. It doesn’t override an attribute on a member.
  • One instance for each options instance. The serializer creates the converter the first time the options need it and keeps it. A converter named on a property or field is created once for that member.
  • The converter is a type argument. [XmlConverter<MoneyConverter>] names the converter, and the compiler checks that it derives from XmlConverter and has a public parameterless constructor. A converter that can’t convert the annotated type still fails at run time: the serializer throws InvalidOperationException when it first needs it.
  • Source generation reads the attribute on a type and on a member. A generated context reads and writes a type that carries [XmlConverter] with that converter, in place of generated metadata for the type. On a property or field, the attribute is carried in the generated metadata, and the converter reads and writes that member.
  • The converter is created without reflection. The converter type is a type argument with a new() constraint, so the attribute creates it directly and trimming and Native AOT need nothing extra.

Constructors

.ctor

Syntax

.ctor Inherited

Inherited from CloudNimble.Text.Xml.Serialization.XmlConverterAttribute
Initializes a new instance of the XmlConverterAttribute class.

Syntax

Remarks

Only XmlConverterAttribute1` derives from this class: a class outside this library cannot.

.ctor Inherited

Inherited from System.Attribute

Syntax

.ctor Inherited

Inherited from object

Syntax

Properties

ConverterType Override

Syntax

Property Value

Type: System.Type

ConverterType Inherited Abstract

Inherited from CloudNimble.Text.Xml.Serialization.XmlConverterAttribute
When overridden in a derived class, gets the type of the converter to use.

Syntax

Property Value

Type: System.Type The type of the converter, which derives from XmlConverter.

TypeId Inherited Virtual

Inherited from System.Attribute

Syntax

Property Value

Type: object

Methods

CreateConverter Inherited Abstract

Inherited from CloudNimble.Text.Xml.Serialization.XmlConverterAttribute
Creates the converter the attribute names.

Syntax

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlConverter A new converter.

Equals Override

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

Equals Inherited Virtual

Inherited from object

Syntax

Parameters

Returns

Type: bool

Equals Inherited

Inherited from object

Syntax

Parameters

Returns

Type: bool

GetCustomAttribute Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute?

GetCustomAttribute Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute?

GetCustomAttribute Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute?

GetCustomAttribute Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute?

GetCustomAttribute Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute?

GetCustomAttribute Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute?

GetCustomAttribute Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute?

GetCustomAttribute Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute?

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetCustomAttributes Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: System.Attribute[]

GetHashCode Override

Inherited from System.Attribute

Syntax

Returns

Type: int

GetHashCode Inherited Virtual

Inherited from object

Syntax

Returns

Type: int

GetType Inherited

Inherited from object

Syntax

Returns

Type: System.Type

IsDefaultAttribute Inherited Virtual

Inherited from System.Attribute

Syntax

Returns

Type: bool

IsDefined Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

IsDefined Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

IsDefined Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

IsDefined Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

IsDefined Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

IsDefined Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

IsDefined Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

IsDefined Inherited

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

Match Inherited Virtual

Inherited from System.Attribute

Syntax

Parameters

Returns

Type: bool

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.XmlConverter
  • CloudNimble.Text.Xml.Serialization.XmlConverterFactory
  • CloudNimble.Text.Xml.XmlSerializerOptions