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> : DerivedTypeMapping, IEnumerable<KeyValuePair<DerivedTypeIdentifier, ITypeShape>>, IEnumerable
Type Parameters
TBase
The 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
alias
DerivedTypeIdentifierIdentifies the type shape being added to the mapping.
typeShape
ITypeShapeRepresents 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
alias
DerivedTypeIdentifierThe alias for the sub-type.
Type Parameters
TDerived
The sub-type.
Exceptions
- ArgumentException
Thrown when
alias
or the Type described bytypeShape
have 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
alias
DerivedTypeIdentifierThe alias for the sub-type.
typeShape
ITypeShape<TDerived>The shape of the sub-type.
Type Parameters
TDerived
The sub-type.
Exceptions
- ArgumentException
Thrown when
alias
or the Type described bytypeShape
have 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
alias
DerivedTypeIdentifierThe alias for the sub-type.
provider
ITypeShapeProvider-
The shape provider of
T
. This will typically be obtained by calling theShapeProvider
static property on a witness class (a class on which GenerateShapeAttribute<T> has been applied).
Type Parameters
TDerived
The sub-type.
Exceptions
- ArgumentException
Thrown when
alias
orTDerived
has 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
alias
DerivedTypeIdentifier- The alias for the sub-type.
Type Parameters
TDerived
The sub-type. TProvider
The witness class that provides a type shape for
TDerived
.
Exceptions
- ArgumentException
Thrown when
alias
or the Type described bytypeShape
have already been added to this mapping.
Get(DerivedTypeIdentifier)
Gets the type shape for a given alias.
protected ITypeShape Get(DerivedTypeIdentifier alias)
Parameters
alias
DerivedTypeIdentifierThe 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
alias
DerivedTypeIdentifierIdentifies the type shape being added to the mapping.
typeShape
ITypeShapeRepresents the structure and characteristics of the type being added.
Remarks
The caller should ensure that the shape's type is actually assignable to TBase
.