Class DerivedShapeMapping<TBase>
- Namespace
- Nerdbank.MessagePack
- Assembly
- Nerdbank.MessagePack.dll
Describes a mapping between a base type and its known sub-types, along with the aliases that identify them.
public class DerivedShapeMapping<TBase> : DerivedTypeUnion, IEnumerable<KeyValuePair<DerivedTypeIdentifier, ITypeShape>>, IEnumerable
Type Parameters
TBaseThe base type or interface that all sub-types derive from or implement.
- Inheritance
-
DerivedShapeMapping<TBase>
- Implements
- Derived
-
DerivedTypeMapping<TBase>
- Inherited Members
Remarks
This class requires a type shape or type shape provider to be provided for each sub-type explicitly. Use DerivedTypeMapping<TBase> for a more convenient API that allows you to add sub-types by their type alone.
Properties
BaseType
Gets the base union type.
public override Type BaseType { get; }
Property Value
Count
Gets the number of subtypes described by the mapping.
public int Count { get; }
Property Value
Methods
Add(DerivedTypeIdentifier, ITypeShape)
Adds a type shape to a mapping using a specified identifier, ensuring that the type has not been previously added.
protected void Add(DerivedTypeIdentifier alias, ITypeShape typeShape)
Parameters
aliasDerivedTypeIdentifierIdentifies the type shape being added to the mapping.
typeShapeITypeShapeRepresents the structure and characteristics of the type being added.
Remarks
The caller should ensure that the shape's type is actually assignable to TBase.
Exceptions
- ArgumentException
Thrown when the type being added has already been included in the mapping.
Add<TDerived>(DerivedTypeIdentifier)
Adds a known sub-type to the mapping.
public void Add<TDerived>(DerivedTypeIdentifier alias) where TDerived : TBase, IShapeable<TDerived>
Parameters
aliasDerivedTypeIdentifierThe alias for the sub-type.
Type Parameters
TDerivedThe sub-type.
Exceptions
- ArgumentException
Thrown when
aliasor the Type described bytypeShapehave already been added to this mapping.
Add<TDerived>(DerivedTypeIdentifier, ITypeShapeProvider)
Adds a known sub-type to the mapping.
public void Add<TDerived>(DerivedTypeIdentifier alias, ITypeShapeProvider provider) where TDerived : TBase
Parameters
aliasDerivedTypeIdentifierThe alias for the sub-type.
providerITypeShapeProvider-
The shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance.
Type Parameters
TDerivedThe sub-type.
Exceptions
- ArgumentException
Thrown when
aliasorTDerivedhas already been added to this mapping.
Add<TDerived>(DerivedTypeIdentifier, ITypeShape<TDerived>)
Adds a known sub-type to the mapping.
public void Add<TDerived>(DerivedTypeIdentifier alias, ITypeShape<TDerived> typeShape) where TDerived : TBase
Parameters
aliasDerivedTypeIdentifierThe alias for the sub-type.
typeShapeITypeShape<TDerived>The shape of the sub-type.
Type Parameters
TDerivedThe sub-type.
Exceptions
- ArgumentException
Thrown when
aliasor the Type described bytypeShapehave already been added to this mapping.
Add<TDerived, TProvider>(DerivedTypeIdentifier)
Adds a known sub-type to the mapping.
public void Add<TDerived, TProvider>(DerivedTypeIdentifier alias) where TDerived : TBase where TProvider : IShapeable<TDerived>
Parameters
aliasDerivedTypeIdentifier- The alias for the sub-type.
Type Parameters
TDerivedThe sub-type. TProviderThe witness class that provides a type shape for
TDerived.
Exceptions
- ArgumentException
Thrown when
aliasor the Type described bytypeShapehave already been added to this mapping.
Get(DerivedTypeIdentifier)
Gets the type shape for a given alias.
protected ITypeShape Get(DerivedTypeIdentifier alias)
Parameters
aliasDerivedTypeIdentifierThe alias.
Returns
- ITypeShape
The type shape.
Exceptions
- KeyNotFoundException
Thrown if the alias is not recognized.
GetEnumerator()
Returns an enumerator that iterates through the collection.
public IEnumerator<KeyValuePair<DerivedTypeIdentifier, ITypeShape>> GetEnumerator()
Returns
- IEnumerator<KeyValuePair<DerivedTypeIdentifier, ITypeShape>>
An enumerator that can be used to iterate through the collection.
Set(DerivedTypeIdentifier, ITypeShape)
Adds or re-sets the type shape in the mapping using a specified identifier.
protected void Set(DerivedTypeIdentifier alias, ITypeShape typeShape)
Parameters
aliasDerivedTypeIdentifierIdentifies the type shape being added to the mapping.
typeShapeITypeShapeRepresents the structure and characteristics of the type being added.
Remarks
The caller should ensure that the shape's type is actually assignable to TBase.