Class TypeRelay

Namespace: AutoFixture.Kernel
Assembly: AutoFixture.dll

Maps a request for one Type to a request for another Type.

public class TypeRelay : ISpecimenBuilder

Inheritance

objectTypeRelay

Implements

ISpecimenBuilder

Inherited Members

object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

Extension Methods

SpecimenFactory.Create<T>(ISpecimenBuilder), SpecimenFactory.CreateMany<T>(ISpecimenBuilder), SpecimenFactory.CreateMany<T>(ISpecimenBuilder, int), SpecimenCommand.Do<T>(ISpecimenBuilder, Action<T>), SpecimenCommand.Do<T1, T2>(ISpecimenBuilder, Action<T1, T2>), SpecimenCommand.Do<T1, T2, T3>(ISpecimenBuilder, Action<T1, T2, T3>), SpecimenCommand.Do<T1, T2, T3, T4>(ISpecimenBuilder, Action<T1, T2, T3, T4>), SpecimenQuery.Get<T, TResult>(ISpecimenBuilder, Func<T, TResult>), SpecimenQuery.Get<T1, T2, TResult>(ISpecimenBuilder, Func<T1, T2, TResult>), SpecimenQuery.Get<T1, T2, T3, TResult>(ISpecimenBuilder, Func<T1, T2, T3, TResult>), SpecimenQuery.Get<T1, T2, T3, T4, TResult>(ISpecimenBuilder, Func<T1, T2, T3, T4, TResult>), CustomizationExtensions.ToCustomization(ISpecimenBuilder)

Constructors

TypeRelay(Type, Type)

Initializes a new instance of the TypeRelay class.

public TypeRelay(Type from, Type to)

Parameters

from Type

The Type from which the TypeRelay instance should map.

to Type

The Type to which the TypeRelay instance should map.

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>();

Remarks

The from and to parameters are used by the `ISpecimenContext)` method to map a request for the from Type into a request for the to Type.

Properties

From

Gets the type which is relayed from.

public Type From { get; }

Property Value

Type

To

Gets the type which is relayed to.

public Type To { get; }

Property Value

Type

Methods

Create(object, ISpecimenContext)

Relays a request for the from Type into a request for the to Type.

public object Create(object request, ISpecimenContext context)

Parameters

request object

The request that describes what to create.

context ISpecimenContext

A context that can be used to create other specimens.

Returns

object

If request is a request for the from Type, an instance of the to Type is returned; otherwise a NoSpecimen instance.

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.

See Also

TypeRelay.TypeRelay(Type, Type)