Extension Methods not Recognized

こ雲淡風輕ζ 提交于 2019-11-28 09:35:17

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):

using System.Linq;

Are you sure the extension method is made public?

Wolfgang Grinfeld

If the Extension method is callable when not using the Extension syntax, use the Format:

this.MyExtensionMethod()

That cleared up my problem of not finding the Extension method of a class in VS2010.

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.

For an example implementation that helped me:

(Note the this keyword that has already been mentioned).

    /// <summary>
    /// Convert current bytes to string
    /// </summary>
    /// <param name="bytes">Current byte[]</param>
    /// <returns>String version of current bytes</returns>
    public static string StringValue(this byte[] currentBytes)
    {
        return string.Concat(Array.ConvertAll(bytes, b => b.ToString("X2")));
    }

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:

using ex = MyApp.Example

Removing the alias worked, but I decided instead to add a duplicate, non-aliased using, which also fixed the problem:

using MyApp.Example
using ex = MyApp.Example

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.

GenericLib - project where extension method on generic enums is defined
BusinessObjectLib - project where enums were defined, references GenericLib
ApplicationUI - project referencing both GenericLib and BusinessObjectLib

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.

Make sure if using templates, your template declaration matches what is declared in the method signature with, "this"..

So,

SomeClass<string, string> test = new SomeClass<string, string>();

extensionMethod<key, val>(this SomeClass<key, Lazy<val>>, string val)
{
}

the extension method will not show up because of the lazy wrapper.

You should be careful about method signature

public static ILoggingBuilder AddCustomizedLogging(this ILoggingBuilder builder, string appInsightsKey)

"this" modifier is required for extension methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!