扩展方法无法识别
当扩展方法存在于导入的程序集中时,需要满足什么条件?我在类库项目中构建了一个,但在引用该库的 Web 项目中无法识别它。库中的所有其他类和方法都是受尊重和可见的,但此扩展方法不是。扩展方法在库中使用时是可见的。
What is necessary to have an extension method honored when it exists in an imported assembly? I built one in a class library project but it is not recognized in my web project which references the library. All the other classes and methods in the library are honored and visible but this extension method is not. The extension method is visible when used within the library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
引用包含具有扩展方法的类的程序集是不够的。您需要在要使用扩展方法的每个源文件中导入包含该类的命名空间。
例如,要使用 LINQ 到对象,您需要引用 System.Core 程序集并导入 System.Linq 命名空间(其中包含带有 LINQ 的 Enumerable 类)扩展方法):
Referencing an assembly containing a class with extension methods is not enough. You need to import the namespace containing the class in each of your source file where you want to use the extension methods.
For example, to use LINQ-to-objects, you need to reference the System.Core assembly and import the System.Linq namespace (which contains the Enumerable class with the LINQ extension methods):
如果在不使用扩展语法时扩展方法是可调用的,请使用格式:
这解决了我在 VS2010 中找不到类的扩展方法的问题。
If the Extension method is callable when not using the Extension syntax, use the Format:
That cleared up my problem of not finding the Extension method of a class in VS2010.
您确定扩展方法已公开吗?
Are you sure the extension method is made public?
对于在 VB.NET 中遇到相同问题的任何人,请注意,不仅扩展方法,而且 模块本身也需要标记为
Public
,否则你会得到这个错误。至少 VS2015 Community 是这样,其他版本也可能是这样。For anyone who lands here while having the same problem in VB.NET, note that not only the extension method, but the module itself needs to be marked as
Public
or else you'll get this error. This is the case at least with VS2015 Community and will likely be the case in other versions too.确保如果使用模板,您的模板声明与方法签名中声明的内容匹配,“this”。
因此,
由于惰性包装器,扩展方法将不会显示。
Make sure if using templates, your template declaration matches what is declared in the method signature with, "this"..
So,
the extension method will not show up because of the lazy wrapper.
对于对我有帮助的示例实现:(
请注意已经提到的
this
关键字)。For an example implementation that helped me:
(Note the
this
keyword that has already been mentioned).对于任何想知道的人来说,我也遇到了同样的问题,但没有一个答案有效。事实证明,这是因为程序集的
using
语句有别名:删除别名有效,但我决定添加一个重复的、非别名的
using
,这也修复了问题:For anyone wondering, I had this same problem none of the answers worked. Turns out it was because the
using
statement of the assembly was aliased:Removing the alias worked, but I decided instead to add a duplicate, non-aliased
using
, which also fixed the problem:我在解决方案中对彼此引用的枚举使用扩展方法时遇到了这个问题,如下所示。 Intellisense 适用于 ApplicationUI 项目中的扩展方法,甚至运行时没有出现编译或运行时错误。但这个方法根本行不通。此外,立即窗口向我保证 BusinessObjectLib.MyEnum 不包含具有我的扩展方法名称的方法,并且找不到扩展方法。
尽管解决方案资源管理器看起来可以正常查看 ApplicationUI 中的所有项目,但当我打开 BusinessObjectLib 项目时,我可以看到它对 GenericLib 的引用由于某种原因被破坏。 (就像我们所有的代码一样,VS 可能也有错误?)。首先,我在直接在 VS 中打开的 BusinessObjectLib 项目中工作,删除引用,然后删除项目,然后以相反的顺序恢复这两个项目。然后我重命名了ApplicationUI.sou 文件并强制重建它。我能够通过这些操作解决这个问题,但只有 .sou 文件重命名似乎可以解决问题。立即窗口仍然继续给我同样的错误,但至少运行时代码再次工作。我在其他几个项目中使用了这种确切的模式,而没有遇到我在这里遇到的问题。
I had this problem using an extension method on enums in solutions referenced each other as shown below. Intellisense worked for the extension method in the ApplicationUI project and it even ran without compile or run-time errors. But the method simply didn't work. Also, the immediate window assured me that BusinessObjectLib.MyEnum did not contain a method with the name of my extension method, and no extension method could be found.
Even though, the Solution Explorer looked OK viewing all projects from ApplicationUI, when I opened the BusinessObjectLib project, I could see its reference to GenericLib was broken for some reason. (Like all of our code, VS probably has bugs as well?). First, I worked in the BusinessObjectLib project opened directly in VS, removing the ref, then removing the project, then restoring both in the opposite order. Then I renamed the ApplicationUI.sou file and forced it to be rebuilt. I was able to fix this problem through these actions, but only the .sou file rename seemed to do the trick. The immediate window still continues to give me the same error, but at least the run-time code works again. I am using this exact pattern in several other projects without having the kind of problem I'm having here.
就我而言,扩展方法位于引用组件的不同版本的外部引用中。我同步了两个项目的版本并且它有效。
In my case, the Extension method was in an external reference which was referencing a different version of a component. I synchronized versions on both projects and it worked.
您应该小心方法签名
public static ILoggingBuilder AddCustomizedLogging(this ILoggingBuilder builder, string appInsightsKey)
扩展方法需要“this”修饰符
You should be careful about method signature
public static ILoggingBuilder AddCustomizedLogging(this ILoggingBuilder builder, string appInsightsKey)
"this" modifier is required for extension methods
留意动态类型。
有很多答案,但没有一个解决了我的问题,以防其他人面临这个问题,我的情况是第三方库返回“动态”类型,而我的扩展方法正在处理字符串,所以当应用扩展方法时,没有显示编译错误,但在运行时,却出现了。
转换为相同类型的扩展方法解决了问题。
Look out for dynamic types.
There are many answer, and none of them solved my problem, in case someone else faces this, my case the third party library was returning a "
dynamic
" type and my extension method was working over strings, so when applying the extension method, did not show a compilation error, but during runing time, it did.A cast to the same type of my extension method solved the problem.
就我而言,它说该方法未被识别的原因与扩展方法无关,而是同一文件中的错误未显示在 IDE 中 - 对我来说,解决方案是重新启动 IDE,它突然显示实际的我必须修复错误。
In my case it said the reason the method was not recognized had nothing to do with extension methods, but an error in the same file which did not show up in the IDE - For me the solution was restarting the IDE and it suddenly displayed the actual error i had to fix.
我遇到的问题源于以下解决方案结构:
MyExtesntion.cs
。MyExtesntion.cs
的链接文件。这个答案帮助我查明了问题。
I had issues that stemmed from the following solution structure:
MyExtesntion.cs
.MyExtesntion.cs
.This answer helped me pinpoint the problem.
如果在 VB.NET 中使用扩展方法,
我正在使用一些
Option Strict
和Option Infer
都关闭的代码。失败原因
在
Dim dt1 = GetDataTable()
行中,dt1
被声明为Object
。即使Object
类型上没有方法ExtentionMethod
,VB.NET 也会编译代码。解决方案
打开
Option Infer On
,这将导致dt1
根据GetDataTable
的签名被解释为DataTable
> 方法。或者不使用类型推断,而是手动定义变量类型
Dim dt1 As DataTable = GetDataTable()
观察结果
VS2015 没有为失败的代码添加红色下划线。
Dim dt3 As DataTable = dt1.ExtentionMethod( "parameter1", "parameter2")
但是如果我尝试按 F12(转到定义),IDE 会给我“无法导航到符号...” “ 错误。
直到用户报告该错误后我才发现该错误。
If consuming an extension method in VB.NET
I was working with some code where
Option Strict
andOption Infer
were both off.Reason why it failed
In the line
Dim dt1 = GetDataTable()
,dt1
is being declared as anObject
. VB.NET will compile the code even though there is no methodExtentionMethod
on typeObject
.Solution
Either turn
Option Infer On
, which will causedt1
to be interpreted as aDataTable
from the signature of theGetDataTable
method.or do not use type inference, but rather, manually define your variable types
Dim dt1 As DataTable = GetDataTable()
Observations
VS2015 did not red-underline the failing code.
Dim dt3 As DataTable = dt1.ExtentionMethod( "parameter1", "parameter2")
But if I tried to F12 (Go To Definition), the IDE would give me the "cannot navigate to symbol..." error.
I didn't find the error until a user reported it.