2

Possible Duplicate:
Why is the 'this' keyword required to call an extension method from within the extended class

I have an extension method declared in an assembly under a certain namespace, it's a very common helper:

using System;

namespace Common {
    public static class GenericServiceProvider {
        public static T GetService<T>(this IServiceProvider serviceProvider) {
             return (T)serviceProvider.GetService(typeof(T));
        }
    }
}

Now in a different assembly and namespace, I'm trying to access this extension method from a class that implements IServiceProvider:

using System;
using Common;

namespace OtherNamespace {
    class Bar: IServiceProvider {
        void Foo() {
            GetService<IMyService>(); // doesn't compile
            this.GetService<IMyService>(); // compiles
        }
    }
}

As you can see, I cannot call the generic extension method directly, it doesn't even show up in IntelliSense. However, if I add "this" before the call, it works fine.

This is in Visual Studio 2010 using .NET 4.

Is this normal or am I doing something wrong? Thanks.

Community
  • 1
  • 1
Asik
  • 21,506
  • 6
  • 72
  • 131
  • This is the normal way of calling extension methods on the "same class". I sure in a minute somebody give you a nicer/more complete answer :). – nemesv Jun 13 '12 at 17:56
  • you **probabbly** have some ambiguos definition of another method. – Tigran Jun 13 '12 at 17:56
  • 3
    There's a similar question here: http://stackoverflow.com/a/3513036/1174069 – pjumble Jun 13 '12 at 17:57
  • And another duplicate here: http://stackoverflow.com/questions/10696567/using-extension-methods-inside-extended-class-itself – Jirka Hanika Jun 13 '12 at 18:04

3 Answers3

4

Is this normal or am I doing something wrong? Thanks.

This is normal. The extension method has to extend something. The compiler won't search for extension methods unless there is something as the "expression" (to the left of the ."). This is defined in the C# language spec, 7.6.5.2:

7.6.5.2 Extension method invocations

In a method invocation (§7.5.5.1) of one of the forms
expr . identifier ( )
expr . identifier ( args )
expr . identifier < typeargs > ( )
expr . identifier < typeargs > ( args )

Basically, you always need something for expr. In this case, this works, and the compiler can rewrite this as:

C . identifier ( expr ) 
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
1

It is because you can only use extension methods on objects of that type and as your class inherits IServiceProvider, you will have to use this. in this case. I hope i understand your question which would make this your answer, but GetService(); is not possible as the syntax is incorrect for an extension method in any case.

Viezevingertjes
  • 1,507
  • 1
  • 13
  • 25
  • Exactly, and calling it directly would yield something like this GenericServiceProvider.GetService(this); if you didn't use the "extension" part – scottheckel Jun 13 '12 at 18:01
0

The extension methods are for using in instance of objects/Class. In this case, you have a static class, which works like a unique instance of a class.

So, if you create an extension method, an instance of an object will be able to use it.

Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82