YamlPolymorphismOptions Class

Configures polymorphic serialization behavior.

public sealed class YamlPolymorphismOptions

Constructors

NameDescription
YamlPolymorphismOptions()

YamlPolymorphismOptions()

YamlPolymorphismOptions.cs
public YamlPolymorphismOptions()

No documentation is available.

Properties

NameDescription
DerivedTypeMappingsGets runtime-registered derived type mappings, keyed by base type.
DiscriminatorStyleGets or sets how type discriminators are represented.
InferClosedTypePolymorphismGets or sets a value indicating whether derived type registrations are inferred from the compiler-provided metadata of a closed type hierarchy.
TypeDiscriminatorPropertyNameGets or sets the property name used for discriminator-based polymorphism.
UnknownDerivedTypeHandlingGets or sets behavior when an unknown derived type discriminator is encountered.

DerivedTypeMappings

YamlPolymorphismOptions.cs

Gets runtime-registered derived type mappings, keyed by base type.

public IDictionary<Type, IList<YamlDerivedType>> DerivedTypeMappings { get; }

Property value

IDictionary<Type, IList<YamlDerivedType>>

Remarks

Entries registered here are merged with the registrations declared by YamlDerivedTypeAttribute, which take precedence when the same discriminator, tag, or type is registered in both. Registering the same derived type, discriminator, or tag more than once for a base type within the mappings is an error.

This enables cross-project polymorphism where the base type and derived types live in different assemblies (e.g., clean architecture, plugin systems).

The mappings can only be changed until the instance is assigned to YamlSerializerOptions.PolymorphismOptions. The mappings are copied at that point, and the instance becomes read-only: a later change throws NotSupportedException, so it cannot affect options that are already in use.

DiscriminatorStyle

YamlPolymorphismOptions.cs

Gets or sets how type discriminators are represented.

public YamlTypeDiscriminatorStyle DiscriminatorStyle { get; init; }

InferClosedTypePolymorphism

YamlPolymorphismOptions.cs

Gets or sets a value indicating whether derived type registrations are inferred from the compiler-provided metadata of a closed type hierarchy.

public bool InferClosedTypePolymorphism { get; init; }

Property value

bool

Remarks

Each inferred derived type uses its name, without the generic arity suffix, as its discriminator. Inference is skipped for a type that declares explicit registrations, either with YamlDerivedTypeAttribute or in YamlPolymorphismOptions.DerivedTypeMappings.

YamlPolymorphicAttribute.InferClosedTypePolymorphism overrides this value for the type it is applied to.

TypeDiscriminatorPropertyName

YamlPolymorphismOptions.cs

Gets or sets the property name used for discriminator-based polymorphism.

public string TypeDiscriminatorPropertyName { get; init; }

Property value

string

Exceptions

ArgumentException
Value is null or empty.

UnknownDerivedTypeHandling

YamlPolymorphismOptions.cs

Gets or sets behavior when an unknown derived type discriminator is encountered.

public YamlUnknownDerivedTypeHandling UnknownDerivedTypeHandling { get; init; }