YamlTypeInfo class

Provides YAML serialization-related metadata about a type.

Namespace: System.Text.Yaml.Serialization.Metadata ยท Assembly: Styml.dll

public abstract class YamlTypeInfo

Derived: YamlTypeInfo<T>

Provides YAML serialization-related metadata about a type.

PropertiesLink to this heading

ConstructorAttributeProviderLink to this heading

Gets or sets an attribute provider corresponding to the deserialization constructor.

public ICustomAttributeProvider? ConstructorAttributeProvider { get; }
Exception Condition
InvalidOperationException The YamlPropertyInfo instance has been locked for further modification.

When resolving metadata via the built-in resolvers this will be populated with the underlying ConstructorInfo of the serialized property or field.

ConverterLink to this heading

Gets the YamlConverter associated with the current type.

public YamlConverter Converter { get; }

The YamlConverter associated with the type determines the value of YamlTypeInfo.Kind, and by extension the types of metadata that are configurable in the current YAML contract. As such, the value of the converter cannot be changed once a YamlTypeInfo instance has been created.

CreateObjectLink to this heading

Gets or sets a parameterless factory to be used on deserialization.

public Func<object>? CreateObject { get; set; }
Exception Condition
InvalidOperationException The YamlTypeInfo instance has been locked for further modification. -or- A parameterless factory is not supported for the current metadata YamlTypeInfo.Kind.

If set to null, any attempt to deserialize instances of the given type will result in an exception. For contracts originating from DefaultYamlTypeInfoResolver or YamlSerializerContext, types with a single default constructor or default constructors annotated with YamlConstructorAttribute will be mapped to this delegate.

ElementTypeLink to this heading

Gets the element type corresponding to an enumerable, dictionary or optional type.

public Type? ElementType { get; }

Returns the element type for enumerable types, the value type for dictionary types, and the underlying type for Nullable<T> or F# optional types. Returns null for all other types or types using custom converters.

IsReadOnlyLink to this heading

Specifies whether the current instance has been locked for modification.

public bool IsReadOnly { get; }

A YamlTypeInfo instance can be locked either if it has been passed to one of the YamlSerializer methods, has been associated with a YamlSerializerContext instance, or a user explicitly called the YamlTypeInfo.MakeReadOnly method on the instance.

KeyTypeLink to this heading

Gets the key type corresponding to a dictionary type.

public Type? KeyType { get; }

Returns the key type for dictionary types. Returns null for all other types or types using custom converters.

KindLink to this heading

Determines the kind of contract metadata that the current instance is specifying.

public YamlTypeInfoKind Kind { get; }

The value of YamlTypeInfo.Kind determines what aspects of the YAML contract are configurable. For example, it is only possible to configure the YamlTypeInfo.Properties list for metadata of kind YamlTypeInfoKind.Object. The value of YamlTypeInfo.Kind is determined exclusively by the YamlConverter resolved for the current type, and cannot be changed once resolution has happened. User-defined custom converters (specified either via YamlConverterAttribute or YamlSerializerOptions.Converters) are metadata-agnostic and thus always resolve to YamlTypeInfoKind.None.

NumberHandlingLink to this heading

Gets or sets the type-level YamlSerializerOptions.NumberHandling override.

public YamlNumberHandling? NumberHandling { get; set; }
Exception Condition
InvalidOperationException The YamlTypeInfo instance has been locked for further modification.
ArgumentOutOfRangeException Specified an invalid YamlNumberHandling value.

For contracts originating from DefaultYamlTypeInfoResolver or YamlSerializerContext, the value of this callback will be mapped from any YamlNumberHandlingAttribute annotations.

OnDeserializedLink to this heading

Gets or sets a callback to be invoked after deserialization occurs.

public Action<object>? OnDeserialized { get; set; }
Exception Condition
InvalidOperationException The YamlTypeInfo instance has been locked for further modification. -or- Serialization callbacks are only supported for YamlTypeInfoKind.Object metadata.

For contracts originating from DefaultYamlTypeInfoResolver or YamlSerializerContext, the value of this callback will be mapped from any IYamlOnDeserialized implementation on the type.

OnDeserializingLink to this heading

Gets or sets a callback to be invoked before deserialization occurs.

public Action<object>? OnDeserializing { get; set; }
Exception Condition
InvalidOperationException The YamlTypeInfo instance has been locked for further modification. -or- Serialization callbacks are only supported for YamlTypeInfoKind.Object metadata.

For contracts originating from DefaultYamlTypeInfoResolver or YamlSerializerContext, the value of this callback will be mapped from any IYamlOnDeserializing implementation on the type.

OnSerializedLink to this heading

Gets or sets a callback to be invoked after serialization occurs.

public Action<object>? OnSerialized { get; set; }
Exception Condition
InvalidOperationException The YamlTypeInfo instance has been locked for further modification. -or- Serialization callbacks are only supported for YamlTypeInfoKind.Object metadata.

For contracts originating from DefaultYamlTypeInfoResolver or YamlSerializerContext, the value of this callback will be mapped from any IYamlOnSerialized implementation on the type.

OnSerializingLink to this heading

Gets or sets a callback to be invoked before serialization occurs.

public Action<object>? OnSerializing { get; set; }
Exception Condition
InvalidOperationException The YamlTypeInfo instance has been locked for further modification. -or- Serialization callbacks are only supported for YamlTypeInfoKind.Object metadata.

For contracts originating from DefaultYamlTypeInfoResolver or YamlSerializerContext, the value of this callback will be mapped from any IYamlOnSerializing implementation on the type.

OptionsLink to this heading

Gets the YamlSerializerOptions value associated with the current YamlTypeInfo instance.

public YamlSerializerOptions Options { get; }

PolymorphismOptionsLink to this heading

Gets or sets a configuration object specifying polymorphism metadata.

public YamlPolymorphismOptions? PolymorphismOptions { get; set; }
Exception Condition
ArgumentException value has been associated with a different YamlTypeInfo instance.
InvalidOperationException The YamlTypeInfo instance has been locked for further modification. -or- Polymorphic serialization is not supported for the current metadata YamlTypeInfo.Kind.

For contracts originating from DefaultYamlTypeInfoResolver or YamlSerializerContext, the configuration of this setting will be mapped from any YamlDerivedTypeAttribute or YamlPolymorphicAttribute annotations.

PreferredPropertyObjectCreationHandlingLink to this heading

Gets or sets the preferred YamlObjectCreationHandling value for properties contained in the type.

public YamlObjectCreationHandling? PreferredPropertyObjectCreationHandling { get; set; }
Exception Condition
InvalidOperationException The YamlTypeInfo instance has been locked for further modification. -or- Unmapped member handling only supported for YamlTypeInfoKind.Object.
ArgumentOutOfRangeException Specified an invalid YamlObjectCreationHandling value.

For contracts originating from DefaultYamlTypeInfoResolver or YamlSerializerContext, the value of this callback will be mapped from YamlObjectCreationHandlingAttribute annotations on types.

PropertiesLink to this heading

Gets the list of YamlPropertyInfo metadata corresponding to the current type.

public IList<YamlPropertyInfo> Properties { get; }

Property is only applicable to metadata of kind YamlTypeInfoKind.Object. For other kinds an empty, read-only list will be returned. The order of YamlPropertyInfo entries in the list determines the serialization order, unless either of the entries specifies a non-zero YamlPropertyInfo.Order value, in which case the properties will be stable sorted by YamlPropertyInfo.Order. It is required that added YamlPropertyInfo entries are unique up to YamlPropertyInfo.Name, however this will only be validated on serialization, once the metadata instance gets locked for further modification.

TypeLink to this heading

Gets the YamlTypeInfo.Type for which the YAML serialization contract is being defined.

public Type Type { get; }

TypeClassifierLink to this heading

Gets or sets the delegate that classifies YAML payloads to determine the target type during deserialization.

public YamlTypeClassifier? TypeClassifier { get; set; }

This is the shared classification property for both polymorphic types and union types.

For polymorphic types: when set, bypasses the standard discriminator-based type resolution (scanning for a $type property). When null (default), the existing discriminator-based approach is used unchanged.

For union types: determines which case type matches the YAML payload. When set by the user, replaces the default token-based matching.

UnionCasesLink to this heading

Gets the list of union case type metadata for this type.

public IList<YamlUnionCaseInfo> UnionCases { get; }

This property is only meaningful when YamlTypeInfo.Kind is YamlTypeInfoKind.Union. The list is mutable during configuration and frozen at finalization time.

For types recognized as unions via System.Runtime.CompilerServices.UnionAttribute, the list is automatically populated. For contract customization, users can populate this list manually.

UnionConstructorLink to this heading

Gets or sets the weakly-typed delegate that constructs a union instance from a case type and case value.

public Func<Type, object?, object>? UnionConstructor { get; set; }

The delegate takes a YamlTypeInfo.Type parameter naming the resolved case type (as produced by classification) and an object? case value, returning the constructed union instance. The caseType parameter is always non-null and disambiguates among overlapping case types (e.g., Labrador : Dog).

The delegate also encapsulates the union's null-handling policy. When the converter encounters a YAML YamlTokenType.Null token, it bypasses any configured YamlTypeInfo.TypeClassifier and invokes the delegate with a null case value. Implementations should produce the canonical null-holding union instance when at least one case is nullable, and otherwise throw a YamlException. On the null path the caseType argument is set to one of the declared nullable case types but its specific value should be ignored; per union semantics, all nullable cases collapse to the same null instance.

Prefer setting the strongly-typed YamlTypeInfo<T>.UnionConstructor on YamlTypeInfo<T> to avoid boxing when the union type is a value type.

UnionDeconstructorLink to this heading

Gets or sets the weakly-typed delegate that deconstructs a union instance into its case type and case value.

public Func<object, ValueTuple<Type?, object?>>? UnionDeconstructor { get; set; }

The delegate combines object classification and value extraction in a single call, returning a tuple of (Type? CaseType, object? CaseValue).

CaseType doubles as the discriminator for the union's null state:

  • A non-null``CaseType instructs the converter to serialize CaseValue using the YamlTypeInfo registered for that case type. CaseValue may be null, in which case the YAML output is null rendered through that case type's contract (this preserves per-case null annotations). The CaseType must be one of the union's declared case types; returning a runtime type that is more derived than any declared case violates STJ contract invariants.
  • A null``CaseType signals that the instance is the canonical null union value. The converter writes YAML null directly, bypassing all per-case contracts; CaseValue is ignored.

Prefer setting the strongly-typed YamlTypeInfo<T>.UnionDeconstructor on YamlTypeInfo<T> to avoid boxing when the union type is a value type.

UnmappedMemberHandlingLink to this heading

Gets or sets the type-level YamlUnmappedMemberHandling override.

public YamlUnmappedMemberHandling? UnmappedMemberHandling { get; set; }
Exception Condition
InvalidOperationException The YamlTypeInfo instance has been locked for further modification. -or- Unmapped member handling only supported for YamlTypeInfoKind.Object.
ArgumentOutOfRangeException Specified an invalid YamlUnmappedMemberHandling value.

For contracts originating from DefaultYamlTypeInfoResolver or YamlSerializerContext, the value of this callback will be mapped from any YamlUnmappedMemberHandlingAttribute annotations.

MethodsLink to this heading

CreateYamlPropertyInfoLink to this heading

Creates a blank YamlPropertyInfo instance for the current YamlTypeInfo.

public YamlPropertyInfo CreateYamlPropertyInfo(Type propertyType, string name);

Parameters

  • propertyType: The declared type for the property.
  • name: The property name used in YAML serialization and deserialization.

Returns: A blank YamlPropertyInfo instance.

Exception Condition
ArgumentNullException propertyType or name is null.
ArgumentException propertyType cannot be used for serialization.
InvalidOperationException The YamlTypeInfo instance has been locked for further modification.

CreateYamlTypeInfoLink to this heading

Creates a blank YamlTypeInfo<T> instance.

public static YamlTypeInfo<T?> CreateYamlTypeInfo<T>(YamlSerializerOptions options);

Type parameters

  • T: The type for which contract metadata is specified.

Parameters

Returns: A blank YamlTypeInfo<T> instance.

Exception Condition
ArgumentNullException options is null.

The returned YamlTypeInfo<T> will be blank, with the exception of the YamlTypeInfo.Converter property which will be resolved either from YamlSerializerOptions.Converters or the built-in converters for the type. Any converters specified via YamlConverterAttribute on the type declaration will not be resolved by this method. What converter does get resolved influences the value of YamlTypeInfo.Kind, which constrains the type of metadata that can be modified in the YamlTypeInfo instance.

Creates a blank YamlTypeInfo instance.

public static YamlTypeInfo CreateYamlTypeInfo(Type type, YamlSerializerOptions options);

Parameters

  • type: The type for which contract metadata is specified.
  • options: The YamlSerializerOptions instance the metadata is associated with.

Returns: A blank YamlTypeInfo instance.

Exception Condition
ArgumentNullException type or options is null.
ArgumentException type cannot be used for serialization.

The returned YamlTypeInfo will be blank, with the exception of the YamlTypeInfo.Converter property which will be resolved either from YamlSerializerOptions.Converters or the built-in converters for the type. Any converters specified via YamlConverterAttribute on the type declaration will not be resolved by this method. What converter does get resolved influences the value of YamlTypeInfo.Kind, which constrains the type of metadata that can be modified in the YamlTypeInfo instance.

MakeReadOnlyLink to this heading

Locks the current instance for further modification.

public void MakeReadOnly();

This method is idempotent.

Generated from the XML documentation of the libraryBuilt with Douze

Esc to close