views:

151

answers:

2

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.

Thanks

+10  A: 

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;
dtb
I have Using statements in my class files that reference the library and my project already references it. Not sure what else to do.
ChiliYago
Are your extension method and the class containing the method public? Have you double-checked that you didn't forget `this` in the method signature? Does it work if you don't use extension method syntax to call the method?
dtb
You need to find out what namespace the extension class you want is implemented in, and use that namespace in the C# code where you want the extensions available. You have to be very specific.
dthorpe
yes i can see the extension method when NOT using the extension method syntax. The namespace containing the extension method is the same as all other methods in my library. The class and extension method is marked as "public static" and yes "this" is included in the method signature.
ChiliYago
Can you post a short but complete program that demonstrates the problem?
dtb
This there was some type of Visual Studio referencing problem between projects. Deleted and restored references and it cleared up...
ChiliYago
+1  A: 

Are you sure the extension method is made public?

Arjan Einbu
yes the class is marked public static as it the extension method.
ChiliYago