c# - System.Interactive.Async 中 IAsyncEnumerable 的鸭子枚举错误

标签 c# .net system.interactive

我想知道为什么这个代码为 IAsyncEnumerable<>

dynamic duckAsyncEnumerable = new int[0].ToAsyncEnumerable();
var duckAsyncEnumerator = duckAsyncEnumerable.GetEnumerator();

引发异常:

'object' does not contain a definition for 'GetEnumerator'

IEnumerable<> 的代码相同工作正常。 此外实现IAsyncEnumerable<>通过反射也可以正常工作。 在 .NET 和 .NET Core 中复制。

IOutputFormatter 需要此代码将源数据作为对象获取并必须对其进行迭代的实现。

described example in dotnetfiddle

最佳答案

调用new int[0].ToAsyncEnumerable()将返回(内部)类型 AsyncIListEnumerableAdapter<int> 。此类型除其他外还实现了 IEnumerable<int>所以它有方法 IEnumerable<int>.GetEnumerator() 。但是,它使用显式接口(interface)实现来实现此方法。

通过 dynamic 调用时,显式实现的接口(interface)方法不可用。 (这是私有(private)的)。要访问该方法,您必须首先将引用强制转换为接口(interface),如answer to the question Use explicit interface implementations with a dynamic object中所述。 .

关于c# - System.Interactive.Async 中 IAsyncEnumerable 的鸭子枚举错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46112798/

相关文章:

c# - 有没有办法创建通用的 Action 或 Func

c# - 从数据访问层提取业务逻辑

c# - Rx : EnumerableEx. For() 与 Enumerable.SelectMany()

c# - Silverlight 的 Reactive Extensions (Rx) - System.Interactive.dll 移到了哪里?

c# - 如何在 xamarin android 项目上集成 webrtc android native 库?

c# - 将 "this"传递给基础构造函数

.net - 从.NET代码中删除未使用的局部变量

c# - 在其自己的构造函数中将 "this"作为参数传递是一种不好的做法吗?

c# - 某处有 Ix.NET (System.Interactive) 的示例吗?

c# - 如何将数组的一部分复制到新数组。 C#