How to build a TransformManyBlock with a delegate

I'm new to C # TPL and DataFlow, and I'm struggling to figure out how to implement the TPL DataFlow TransformManyBlock . I am translating another code into DataFlow. My (simplified) source code was something like this:

 private IEnumerable<byte[]> ExtractFromByteStream(Byte[] byteStream) { yield return byteStream; // Plus operations on the array } 

And in another method, I would call it like this:

 foreach (byte[] myByteArray in ExtractFromByteStream(byteStream)) { // Do stuff with myByteArray } 

I am trying to create a TransformManyBlock to create several small arrays (actually data packets) that come from a larger input array (actually a binary stream), so both inputs and outputs are of type byte[] .

I tried what I put below, but I know that everything worked out for me. I want to build this block using the same function as before, and just wrap TransformManyBlock around it. I got the error message "The call is ambiguous ..."

 var streamTransformManyBlock = new TransformManyBlock<byte[], byte[]>(ExtractFromByteStream); 
+4
source share
1 answer

The compiler has problems with type inference. You need to either specify the delegate type explicitly to fix this problem:

 var block = new TransformManyBlock<byte[], byte[]>( (Func<byte[], IEnumerable<byte[]>>) ExtractFromByteStream); 

Or you can use the lambda expression that calls this method:

 var block = new TransformManyBlock<byte[], byte[]>( bytes => ExtractFromByteStream(bytes)); 
+5
source

All Articles