YamlUnionAttribute class

Customizes the System.Text.Yaml behavior of a union type.

Namespace: System.Text.Yaml.Serialization · Assembly: Styml.dll

public sealed class YamlUnionAttribute : YamlAttribute

Inheritance: object → Attribute → YamlAttribute → YamlUnionAttribute

Customizes the System.Text.Yaml behavior of a union type.

RemarksLink to this heading

A type is recognized by System.Text.Yaml as a union when it carries System.Runtime.CompilerServices.UnionAttribute (the marker emitted by the C# compiler for union declarations). Applying YamlUnionAttribute on its own does not make a type a union — the attribute is silently ignored when no union marker is present, and the type is serialized as a regular object.

On a recognized union, this attribute is purely a customization knob — currently it lets the user plug in a per-type YamlUnionAttribute.TypeClassifier. Recognized unions without this attribute receive the default YAML-value-based classifier.

Case types are discovered automatically from the union's public single-parameter constructors. For types where convention-based discovery does not work, use contract customization to populate YamlTypeInfo.UnionCases and set the deconstructor/constructor delegates on YamlTypeInfo<T>.

ConstructorsLink to this heading

public YamlUnionAttribute();

PropertiesLink to this heading

TypeClassifierLink to this heading

Gets or sets the type of a YamlTypeClassifierFactory implementation used to classify YAML payloads during deserialization.

public Type? TypeClassifier { get; set; }

When null, the built-in YAML value-shape classifier is used. It dispatches using the current YAML token kind rather than by inspecting object property names or other payload contents.

The specified type must derive from YamlTypeClassifierFactory and have a public parameterless constructor.

The classifier is not invoked for YAML YamlTokenType.Null payloads. The null token is dispatched directly to the union's constructor delegate, which yields the canonical null union when at least one case is nullable and otherwise throws a YamlException. See YamlTypeClassifier for details.

Generated from the XML documentation of the libraryBuilt with Douze

Esc to close