Extension Methods not Recognized

前端 未结 12 1008
抹茶落季
抹茶落季 2020-12-10 00:09

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 proj

相关标签:
12条回答
  • 2020-12-10 01:03

    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.

    var a = thirdpartyLibrary.GetValue().MyExtensionMethod();
    

    A cast to the same type of my extension method solved the problem.

    0 讨论(0)
  • 2020-12-10 01:04

    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.

    0 讨论(0)
  • 2020-12-10 01:04

    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.

    0 讨论(0)
  • 2020-12-10 01:07

    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.

    0 讨论(0)
  • 2020-12-10 01:09

    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;
    
    0 讨论(0)
  • 2020-12-10 01:10

    Are you sure the extension method is made public?

    0 讨论(0)
提交回复
热议问题