Skip to main content

Definition

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

Syntax

Summary

Provides helpers to create and use metadata for XML-serializable types. This class cannot be inherited.

Usage

XmlMetadataServices is infrastructure for source-generated code. The code that the XmlEssentials generator writes into your XmlSerializerContext classes calls these methods to create type info from generated metadata, to write members through type info in the write fast path, and to write the start and end tags of simple elements from bytes prepared at compile time. CreateObjectInfo turns an XmlObjectInfoValues<T> into type info. The serializer then reads and writes the type from that metadata with the engine the reflection resolver uses, and with no reflection over the members of the type. CanUseSerializeHandler tells a context whether its write fast path agrees with a given options instance. The class isn’t meant to be called from application code. Its methods are public because generated code lives in your assembly and has to reach them, and most of them are hidden from IntelliSense. WriteLeafStart and WriteLeafEnd write a tag as raw bytes and rely on the generator to pass a well-formed one. A resolver you write by hand can call CreateObjectInfo and the other Create methods, as a generated context does. To do the same things in application code, use the public entry points instead: The class is the counterpart of JsonMetadataServices in System.Text.Json. To see how generated code is produced and used, see How to use source generation.

Remarks

Source-generated contexts call these methods. This class is analogous to JsonMetadataServices in System.Text.Json.

Methods

CanUseSerializeHandler

Returns whether the write fast path a context generated can be used with the given options.

Syntax

Parameters

Returns

Type: bool true when the fast path writes what the serializer would write from the metadata with these options; otherwise, false.

Exceptions

Remarks

The fast path has its element and attribute names and its choice of members compiled in, from XmlSourceGenerationOptionsAttribute. It is not used when the options ask for another naming policy, property mapping, collection mapping or choice of members, when they handle references, when they write a null, a Char or a zero offset another way, when they put fields first, or when they supply a converter for a type it writes directly. The serializer then writes from the metadata, which follows every option. This method is for source-generated contexts only.

CreateArrayInfo

Creates metadata for an array type whose item type is named at compile time.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlTypeInfo<TElement[]> The serialization metadata for an array of TElement.

Type Parameters

  • TElement - The type of the items.

Exceptions

Remarks

Generated contexts call this method instead of closing a generic converter through reflection, which Native AOT cannot do for an item type that is a value type. A converter the caller supplied for the array type is used in place of the built-in one.

CreateArrayPropertyInfo

Marks the metadata of an array member with the type of its items.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlPropertyInfoValues<TElement[]> propertyInfo, which now reads and writes the member as an array of TElement.

Type Parameters

  • TElement - The type of the items.

Exceptions

Remarks

An array has its own method because XmlMetadataServices.CreateCollectionPropertyInfo keeps the constructors and methods of the collection type for the trimmer, and those of Array cannot be kept under Native AOT. An array needs none of them: it is made from the items that were read. This method is for source-generated contexts only.

CreateBuiltInInfo

Creates type info for a type the serializer has a ready-made converter for, or that the caller supplied a converter for. Source-generated code calls this method.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlTypeInfo<T> The type info.

Type Parameters

  • T - The type: a built-in value such as Int32 or String, or one a converter of the caller’s reads and writes.

Exceptions

Remarks

Nothing is looked up by reflection and no generic type is closed at run time, so the call is safe under trimming and Native AOT.

CreateCollectionInfo

Creates metadata for a collection type whose item type is named at compile time.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlTypeInfo<TCollection> The serialization metadata for type TCollection.

Type Parameters

  • TCollection - The collection type.
  • TElement - The type of the items.

Exceptions

Remarks

Generated contexts call this method instead of closing a generic converter through reflection, which Native AOT cannot do for an item type that is a value type. A converter the caller supplied for the collection type is used in place of the built-in one.

CreateCollectionPropertyInfo

Marks the metadata of a collection member with the type of its items.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlPropertyInfoValues<TCollection> propertyInfo, which now reads and writes the member as a collection of TElement.

Type Parameters

  • TCollection - The declared type of the member.
  • TElement - The type of the items.

Exceptions

Remarks

The serializer maps a collection through a type that is generic over the item type. This method closes it where both types are type arguments, so nothing is closed at run time, which Native AOT cannot do for a value-type item. This method is for source-generated contexts only.

CreateDictionaryInfo

Creates metadata for a dictionary type whose key and value types are named at compile time.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlTypeInfo<TDictionary> The serialization metadata for type TDictionary.

Type Parameters

  • TDictionary - The dictionary type.
  • TKey - The type of the keys.
  • TValue - The type of the values.

Exceptions

Remarks

Generated contexts call this method instead of closing a generic converter through reflection, which Native AOT cannot do for a key or value type that is a value type. A converter the caller supplied for the dictionary type is used in place of the built-in one.

CreateEnumInfo

Creates metadata for an enumeration type.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlTypeInfo<T> The serialization metadata for type T.

Type Parameters

  • T - The enumeration type.

Exceptions

Remarks

Generated contexts call this method for an enumeration they list or reach. A converter the caller supplied for the enumeration, in the options or with XmlConverterAttribute, is used in place of the built-in one.

CreateException

Creates the exception that generated code throws for an error it finds while it writes a value.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.XmlException An XmlException that the serializer treats as one of its own: when the serializer gives it a XmlException.Path, the path follows the message.

Exceptions

Remarks

An exception that is created with a constructor of XmlException keeps the message it was given. Generated code uses this method so that an error reads the same whether a write fast path or the serializer found it.

CreateNullableInfo

Creates metadata for Nullable`1 of a value type that is not one of the built-in scalars.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlTypeInfo<T?> The serialization metadata for the nullable form of type T.

Type Parameters

  • T - The value type.

Exceptions

Remarks

Generated contexts call this method for the nullable form of an enumeration or a struct. The value is read and written by the converter the options have for T, and null is handled around it.

CreateNullablePropertyInfo

Marks the metadata of a member whose type is a nullable value type and that names its own converter.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlPropertyInfoValues<T?> propertyInfo, which now wraps the member’s converter so that it serves the nullable type.

Type Parameters

  • T - The underlying value type, which the converter converts.

Exceptions

Remarks

A converter for T named by XmlConverterAttribute on a member of type T? converts the value, and null is handled around it. This method closes that wrapper where T is a type argument. This method is for source-generated contexts only.

CreateObjectInfo

Creates metadata for an object type from the metadata of its constructor and members.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlTypeInfo<T> Type info that reads and writes T with the rules the serializer applies to a type it maps through reflection.

Type Parameters

  • T - The object type.

Exceptions

Remarks

The names, the member order, the ignore conditions, the constructor mapping, polymorphism, reference handling and every other option are applied here, by the code that applies them for the reflection resolver. A converter the caller supplied for T is used in place of the metadata. The members of T are never reflected over, so the result is usable in a trimmed application and under Native AOT. This method is for source-generated contexts only. It is analogous to JsonMetadataServices.CreateObjectInfo<T> in System.Text.Json.

CreateValueInfo

Creates metadata for a type that is read and written with the specified converter.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlTypeInfo<T> The serialization metadata for type T.

Type Parameters

  • T - The type that the metadata describes.

Exceptions

CreateValuePropertyInfo

Marks the metadata of a member whose type is a value type.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlPropertyInfoValues<TValue> propertyInfo, which can now hold a value for a constructor without boxing it again for each object.

Type Parameters

  • TValue - The type of the member.

Exceptions

Remarks

When a type is created through a constructor, the serializer holds each value until the element ends. A value type is held in one box that is written again for the next object. That box is generic over the member’s type, and this method closes it where the type is a type argument. This method is for source-generated contexts only.

GetBuiltInTypeInfo

Gets type info for a type whose converter exists already: a built-in scalar or its nullable form, an XML value, Object, or a type the caller supplied a converter for.

Syntax

Parameters

Returns

Type: CloudNimble.Text.Xml.Serialization.XmlTypeInfo? The type info, or null when a converter would have to be made for type.

Exceptions

Remarks

A generated context calls this method for a type it neither lists nor reaches. It answers null for an enumeration, a collection, a dictionary, the nullable form of a value type that is not a built-in scalar, and a type mapped member by member. Making a converter for one of those takes reflection or closes a generic type at run time, which Native AOT cannot do. The next resolver in XmlSerializerOptions.TypeInfoResolverChain is then asked, and a type nobody lists is reported as not supported.

RegisterClosedDerivedType

Tells the serializer that a generic derived type of a closed type takes the type arguments of that closed type. Source-generated code calls this method.

Syntax

Type Parameters

  • TClosed - The closed type, with its type arguments.
  • TDerived - The derived type, with the same type arguments.

Remarks

The compiler lists a generic derived type of a closed type without its type arguments. A generic type cannot be given its arguments at run time under Native AOT, so the context names the constructed type. A closed type needs .NET 11, so the method exists only there.

RegisterCollection

Tells the serializer that TCollection is a collection of TElement. Source-generated code calls this method.

Syntax

Type Parameters

  • TCollection - The collection type.
  • TElement - The type of its items.

Remarks

The reflection resolver finds the item type through the interfaces of the collection type, which may be trimmed away. A context names both types when it is created, so they are known before any type that has such a member is bound.

RegisterDictionary

Tells the serializer that TDictionary is a dictionary of TKey and TValue. Source-generated code calls this method.

Syntax

Type Parameters

  • TDictionary - The dictionary type.
  • TKey - The type of its keys.
  • TValue - The type of its values.

Remarks

The reflection resolver finds the key and value types through the interfaces of the dictionary type, which may be trimmed away. A context names the types when it is created.

Write

Writes the specified value as the content of the current element, by using the specified metadata.

Syntax

Parameters

Type Parameters

  • T - The type of the value to write.

Exceptions

Remarks

The method calls XmlConverter`1.Write on the converter of typeInfo. It does not write the start tag or the end tag.

WriteAttribute

Writes the specified value as an attribute with the specified name, by using the specified metadata.

Syntax

Parameters

Type Parameters

  • T - The type of the value to write.

Exceptions

WriteCData

Writes text as CDATA, in as many sections as it takes.

Syntax

Parameters

Exceptions

Remarks

A CDATA section ends at the first ]]>, so text that holds one is cut between the brackets and the angle bracket and written as adjacent sections, which read back as one value. This method is for the write fast path of a source-generated context only.

WriteLeafEnd

Ends a leaf element that was started with XmlMetadataServices.WriteLeafStart, by writing an end tag that was built ahead of time.

Syntax

Parameters

Exceptions

Remarks

This method is for the write fast path of a source-generated context only.

WriteLeafStart

Starts a leaf element by writing a start tag that was built ahead of time.

Syntax

Parameters

Exceptions

Remarks

This method is for the write fast path of a source-generated context only.
  • CloudNimble.Text.Xml.Serialization.XmlSerializerContext
  • CloudNimble.Text.Xml.Serialization.XmlTypeInfo
  • CloudNimble.Text.Xml.Serialization.XmlConverter
  • CloudNimble.Text.Xml.Serialization.XmlSerializableAttribute