Class CastTransformer<TSource, TDestination>

Namespace
Wolfgang.Etl.Transformers
Assembly
Wolfgang.Etl.Transformers.dll

A transformer that casts each input item to TDestination, throwing InvalidCastException if any item is not of that type.

public sealed class CastTransformer<TSource, TDestination> : ITransformAsync<TSource, TDestination> where TSource : notnull where TDestination : notnull

Type Parameters

TSource

The type of items in the input sequence. Must be non-null.

TDestination

The type to cast to. Must be non-null. No compile-time relationship with TSource is required - the cast is performed at runtime.

Inheritance
CastTransformer<TSource, TDestination>
Implements
ITransformAsync<TSource, TDestination>
Inherited Members
Extension Methods

Examples

// narrow a heterogeneous object stream to strings, asserting that every item is one
var strings = new CastTransformer<object, string>();

// downcast every Animal to Dog (assumes the upstream guarantees this)
var dogs = new CastTransformer<Animal, Dog>();

Remarks

CastTransformer<TSource, TDestination> is the transformer equivalent of LINQ's Cast<TResult>(IEnumerable): it casts every input item to TDestination and throws InvalidCastException if any item does not have a runtime conversion to that type.

Use OfTypeTransformer<TSource, TDestination> instead when mismatched items should be silently filtered out rather than raise an exception.

Implements only Wolfgang.Etl.Abstractions.ITransformAsync<TSource, TDestination> - no progress, no cancellation, no Skip/Max - to keep the hot loop minimal.

Constructors

CastTransformer()

Initializes a new instance of the CastTransformer<TSource, TDestination> class.

public CastTransformer()

Methods

TransformAsync(IAsyncEnumerable<TSource>)

Asynchronously yields each item from items cast to TDestination.

public IAsyncEnumerable<TDestination> TransformAsync(IAsyncEnumerable<TSource> items)

Parameters

items IAsyncEnumerable<TSource>

The asynchronous source sequence.

Returns

IAsyncEnumerable<TDestination>

An asynchronous sequence of the same items, cast to TDestination.

Exceptions

ArgumentNullException

items is null.

InvalidCastException

An item in items is not assignable to TDestination.