Calling a method as an extension method requires more references than calling it directly

I have an extension method in what I will call HelperAssembly , which looks something like this:

public static class HelperClass { public static void MyHelperMethod(this SomeClass some, OtherClass other) { // This object is defined in OtherAssembly1 ObjectFromOtherAssembly1 object1 = new ObjectFromOtherAssembly1(some); // This object is defined in OtherAssembly2 ObjectFromOtherAssembly2 object2 = new ObjectFromOtherAssembly2(other); DoStuffWith(object1, object2); } } 

I have an assembly, which I will call CallerAssembly , which has a reference to HelperAssembly .

My HelperAssembly has a link to both OtherAssembly1 and OtherAssembly2 .

SomeClass and OtherClass are defined in ReferenceAssembly . HelperAssembly and CallerAssembly have a reference to ReferenceAssembly .

Everything is fine when I call my method from CallerAssembly as follows:

 HelperClass.MyHelperMethod(some, other); 

However, I get build errors when I call it (as an extension method):

 some.MyHelperMethod(other); 

Errors say that CallerAssembly needs to be referenced by OtherAssembly1 and OtherAssembly2 .

I'm confused. I thought the syntax of the extension method was just syntactic sugar, but didn't really change the way things were compiled.

I do not want to add the links that it offers, so I will not make the call as an extension method. But I would like to understand what the difference is.

Why does calling a method directly build, but calling it as an extension method fails to build?

+7
source share
1 answer

Any chance CallerAssembly defines a .NET Framework client profile and HelperAssembly targets a “full” .NET Framework?

I had problems with extension methods (and others) when I did this.

+1
source

All Articles