Class TypeRelay
Maps a request for one System.Type to a request for another Type.
Inheritance
Implements
Inherited Members
Namespace: AutoFixture.Kernel
Assembly: AutoFixture.dll
Syntax
public class TypeRelay : ISpecimenBuilder
Constructors
| Improve this Doc View SourceTypeRelay(Type, Type)
Initializes a new instance of the TypeRelay class.
Declaration
public TypeRelay(Type from, Type to)
Parameters
Type | Name | Description |
---|---|---|
System.Type | from | The System.Type from which the TypeRelay instance should map. |
System.Type | to | The System.Type to which the TypeRelay instance should map. |
Remarks
The from
and to
parameters are used by the
Create(Object, ISpecimenContext) method to map a request for the from Type into a request
for the to Type.
Examples
In this example, BaseType is an abstract base class, and DerivedType is a concrete class that derives from BaseType. The Fixture instance is configured to relay all requests for BasetType to requests for DerivedType, so the actual result return from the fixture when BasetType is requested is a DerivedType instance.
var fixture = new Fixture();
fixture.Customizations.Add(
new TypeRelay(
typeof(BaseType),
typeof(DerivedType)));
var actual = fixture.Create<BaseType>();
Properties
| Improve this Doc View SourceFrom
Gets the type which is relayed from.
Declaration
public Type From { get; }
Property Value
Type | Description |
---|---|
System.Type |
To
Gets the type which is relayed to.
Declaration
public Type To { get; }
Property Value
Type | Description |
---|---|
System.Type |
Methods
| Improve this Doc View SourceCreate(Object, ISpecimenContext)
Relays a request for the from Type into a request for the to Type.
Declaration
public object Create(object request, ISpecimenContext context)
Parameters
Type | Name | Description |
---|---|---|
System.Object | request | The request that describes what to create. |
ISpecimenContext | context | A context that can be used to create other specimens. |
Returns
Type | Description |
---|---|
System.Object | If |
Remarks
The from and to Types are defined by constructor arguments. An exact
match is performed when evaluating request
against the from
Type - i.e. derived types are not regarded as matches.
If the request matches the from Type, an instance of the to Type is
requested from the context
.