Class OfTypeTransformer<TSource, TDestination>
- Namespace
- Wolfgang.Etl.Transformers
- Assembly
- Wolfgang.Etl.Transformers.dll
A transformer that yields each input item that is of type TDestination,
silently skipping items of any other type.
public sealed class OfTypeTransformer<TSource, TDestination> : ITransformAsync<TSource, TDestination> where TSource : notnull where TDestination : notnull
Type Parameters
TSourceThe type of items in the input sequence. Must be non-null.
TDestinationThe type to filter for. Must be non-null. No compile-time relationship with
TSourceis required - the check is a runtimeistest.
- Inheritance
-
OfTypeTransformer<TSource, TDestination>
- Implements
-
ITransformAsync<TSource, TDestination>
- Inherited Members
- Extension Methods
Examples
// pull the strings out of a heterogeneous object stream
var strings = new OfTypeTransformer<object, string>();
// narrow an animal stream to only the dogs
var dogs = new OfTypeTransformer<Animal, Dog>();
Remarks
OfTypeTransformer<TSource, TDestination> is the transformer equivalent of LINQ's
OfType<TResult>(IEnumerable):
it filters and casts in a single step, yielding only items that satisfy
item is TDestination.
Items that do not match TDestination are silently skipped - this
transformer never throws InvalidCastException. Use
CastTransformer<TSource, TDestination> instead if mismatched items should
raise an exception rather than be filtered out.
null items (which can technically appear if the source bypasses the
notnull constraint via null!) are also skipped, since null is T is
false for any T.
Implements only Wolfgang.Etl.Abstractions.ITransformAsync<TSource, TDestination> - no progress, no cancellation, no Skip/Max - to keep the hot loop minimal.
Constructors
OfTypeTransformer()
Initializes a new instance of the OfTypeTransformer<TSource, TDestination> class.
public OfTypeTransformer()
Methods
TransformAsync(IAsyncEnumerable<TSource>)
Asynchronously yields each item from items that is an instance of
TDestination.
public IAsyncEnumerable<TDestination> TransformAsync(IAsyncEnumerable<TSource> items)
Parameters
itemsIAsyncEnumerable<TSource>The asynchronous source sequence.
Returns
- IAsyncEnumerable<TDestination>
An asynchronous sequence containing only those input items that are of type
TDestination, cast to that type.
Exceptions
- ArgumentNullException
itemsis null.