自定义扩展方法与框架扩展方法发生冲突。为什么?
我在我们的应用程序中有一个奇怪的行为。我想迭代表 (DataTable
) 的行。我想使用 DataTableExtensions
类中的 AsEnumerable()
扩展方法。类似这样的事情:
foreach(var thing in table.AsEnumerable())
{
...
}
编译时,它抱怨我需要引用一些 ESRI DLL(GIS 应用程序)。经过一番挖掘,我从引用的 DLL 中发现了一些扩展方法,这些方法扩展了 ESRI 中的一些类型(例如 IEnumField
、IEnumLayer
等)。
显然,一个 DataTable
与它们完全不同,我似乎无法找到为什么它试图绑定到 AsEnumerable(this IEnumLayer)
而不是 AsEnumerable(this DataTable)< /代码>。有趣的是,我们正在使用Resharper(很棒的工具!)并且 Resharper 就在我们身边:当您导航到定义时,它会将您带到
AsEnumerable(this DataTable) 在对象浏览器中。
如果不发布数千行专有代码,我无法在这里发布太多代码示例,因此我只是在寻找我曾经遇到过同样的问题,然后我用...类型的答案修复了它。
显而易见的解决方案是通过删除该命名空间的所有 using 语句来删除对扩展方法的任何引用。但这会产生令人讨厌的副作用,迫使我们完全限定任何类型声明。不漂亮。
预先感谢您的任何意见。
埃里克.
I have a strange behavior in our application. I want to iterate over the rows of a table (DataTable
). I wanted to use the AsEnumerable()
extension method from DataTableExtensions
class. Sonmething like:
foreach(var thing in table.AsEnumerable())
{
...
}
When it compiles, it complains that I need to reference some ESRI DLLs (GIS application). After some digging, I found a few extension methods from a referenced DLL that extend some of the types from ESRI (e.g. IEnumField
, IEnumLayer
, etc.)
Obviously, a DataTable
is nothing like what those are and I can't seem to find why it is trying to bind to AsEnumerable(this IEnumLayer)
instead of AsEnumerable(this DataTable)
. The funny thing is also that we are using Resharper (awesome tool!) and Resharper is on our side: when you navigate to the definition, it takes you to AsEnumerable(this DataTable)
in the object browser.
I can't post much code sample here without posting thousands of lines of proprietary code so I am just looking for a I had the same issue once and I fixed it with... type of answer.
The obvious solution was to remove any reference to the extension methods by removing all using statements for that namespace. But this has the nasty side effect of forcing us to fully qualify any type declaration. Not pretty.
Thanks in advance for any input.
Eric.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
using [alias = ]class_or_namespace;
的 alias = 部分。然后你会得到类似using my = System.Data;
的东西,然后将它与my.DataTable.AsEnumerable();
一起使用You could use the alias = part of the
using [alias = ]class_or_namespace;
. Then you would have something likeusing my = System.Data;
and then use it withmy.DataTable.AsEnumerable();
尝试一下....
Try that....
我不太确定为什么会遇到这个问题,但一种潜在的解决方法是放弃扩展方法语法糖,只处理
AsEnumerable
作为普通静态方法:I'm not really sure why you're hitting this problem, but one potential workaround would be to forgo the extension method syntactic sugar and just treat
AsEnumerable
as a normal static method:你能发布你的类层次结构吗?
以下代码显示选择了最派生的编译时类型扩展方法。
它适用于您的代码吗?
使用了这个类:
Can you post your class hierarchy?
The following code shows that the most derived compile-time type Extension Method is choosen.
Does it applies to your code?
Used this classes:
也许以下方法效果最好:
Perhaps the following would work best: