I wonder why this code for IAsyncEnumerable<>
dynamic duckAsyncEnumerable = new int[0].ToAsyncEnumerable();
var duckAsyncEnumerator = duckAsyncEnumerable.GetEnumerator();
raises an exception:
'object' does not contain a definition for 'GetEnumerator'
Same code for IEnumerable<>
works fine.
Moreover inplementation for IAsyncEnumerable<>
via reflection works fine too.
Reproduced in .NET and .NET Core.
This code needed for IOutputFormatter
implementation that get source data as object and have to iterate through it.
Calling
new int[0].ToAsyncEnumerable()
will return the (internal) typeAsyncIListEnumerableAdapter<int>
. This type implements among other thingsIEnumerable<int>
so it has the methodIEnumerable<int>.GetEnumerator()
. However, it implements this method using explicit interface implementation.An interface method that is explicitly implemented is not available when you call through
dynamic
(it is private). To access the method you will have to cast the reference to the interface first as explained in this answer to the question Use explicit interface implementations with a dynamic object.