为什么动态类型的扩展方法无法解析?
可能的重复:
C# 中的扩展方法和动态对象
例如:
var obj = new byte[] { 1, 2, 3 };
dynamic dobj = obj;
dobj.Count(); // fails
Enumerable.Count(dobj); // works
Possible Duplicate:
Extension method and dynamic object in c#
For example:
var obj = new byte[] { 1, 2, 3 };
dynamic dobj = obj;
dobj.Count(); // fails
Enumerable.Count(dobj); // works
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不起作用,因为知道要调用哪个扩展方法需要知道源代码在编译之前是什么样子(包括知道存在哪些
using
指令)。 At runtime this information is not available. The workaround you are using is a good approach.It doesn't work because knowing which extension method to call requires knowing what the source code looked like before it was compiled (including knowing which
using
directives were present). At runtime this information is not available. The workaround you are using is a good approach.