Skip to main content

Definition

Assembly: CloudNimble.XmlEssentials.dll Namespace: CloudNimble.Text.Xml.Serialization Inheritance: System.Attribute

Syntax

Summary

Instructs the XmlEssentials source generator to generate source code for serializing and deserializing instances of the specified type and the types in its object graph. This class cannot be inherited.

Usage

Apply [XmlSerializable] to a partial class that derives from XmlSerializerContext, once for each type you pass to the serializer. The source generator reads the attributes at compile time and adds a matching XmlTypeInfo<T> property to the class.
List the root types only. The generator follows the properties of each type, so listing Order also covers the OrderLine type of its Lines property, and the context gets an OrderLine property too. The generated property is named after the type. Set TypeInfoPropertyName to choose another name, for example when two listed types have the same name in different namespaces:
The code produces the following output:
View the complete sample on GitHub The attribute is the counterpart of JsonSerializableAttribute in System.Text.Json.

Remarks

Place the attribute on a partial class that derives from XmlSerializerContext. The type is a type argument, so the compiler checks it and it may be any closed type, an array or a collection included. This attribute is analogous to JsonSerializableAttribute in System.Text.Json, which takes the type as a constructor argument.

Type Parameters

  • T - The type to generate source code for.

Examples

Considerations

  • The class must be partial and derive from XmlSerializerContext. Otherwise the generator reports XMLESS001.
  • GenerationMode overrides the mode of the context for the listed type. A value other than Default applies to the type the attribute lists. A type listed with Default, and a type the generator reaches from a listed type, takes the mode from [XmlSourceGenerationOptions] on the context.
  • Metadata and a write fast path are generated only for object types. For an enumeration, a collection, or a dictionary you list, such as List<Order>, the property returns type info that the runtime library creates from the type arguments the generator names, with no reflection. For a simple value you list, such as int, the property returns the built-in type info.
  • A listed type is checked at compile time. See Understand the diagnostics for the errors the generator reports.
  • The attribute does nothing at run time. The reflection resolver doesn’t read it.

Constructors

.ctor

Syntax

.ctor Inherited

Inherited from System.Attribute

Syntax

.ctor Inherited

Inherited from object

Syntax

Properties

GenerationMode

Gets or sets the mode that indicates what the source generator generates for the type.

Syntax

Property Value

Type: CloudNimble.Text.Xml.Serialization.XmlSourceGenerationMode A bitwise combination of the enumeration values that specifies what is generated for the type. The default is XmlSourceGenerationMode.Default.

Remarks

A value other than XmlSourceGenerationMode.Default overrides the XmlSourceGenerationOptionsAttribute.GenerationMode of the context for this type. The types reached from it take the mode of the context.

Type

Gets the type to generate source code for.

Syntax

Property Value

Type: System.Type The type that the context can serialize and deserialize, which is T.

TypeId Inherited Virtual

Inherited from System.Attribute

Syntax

Property Value

Type: object

TypeInfoPropertyName

Gets or sets the name of the property for the generated XmlTypeInfo1` for the type on the generated, derived XmlSerializerContext type.

Syntax

Property Value

Type: string? The name of the generated property. If the value is null or empty, the name is made from the name of the type and the names of its type arguments. The default is null.

Methods

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.XmlSerializerContext
  • CloudNimble.Text.Xml.Serialization.XmlSourceGenerationOptionsAttribute
  • CloudNimble.Text.Xml.Serialization.XmlSourceGenerationMode
  • CloudNimble.Text.Xml.Serialization.XmlTypeInfo