I would like to create controller extension method. What I got so far is below
public ActionResult Foo()
{
this.ExtensionMethod();
return View();
}
public static void ExtensionMethod(this Controller controller)
{
}
What I don’t like is that ExtensionMethod must be called with this keyword. Is it possible to get rid of this?
No.
It is the
thiskeyword that makes the method an extension method. Without it, it’s just a static method.Edit: Sorry, I misread the question. There are two
thiskeywords: one in the extension method, and one used to call it.The reason you need the
thiskeyword when you call it is that you need to specify the object that is being extended. C# doesn’t automatically resolve local method calls to extension methods unless you specify thethiskeyword.