Possible Duplicate:
Why is the 'this' keyword required to call an extension method from within the extended class
I want to localization my MVC application then I want to use w(message) method and as
“flem” answered here I have this code:
namespace System.Web.WebPages
{
public static class Localization
{
public static string w(this WebPageBase page, string message)
{
return message;
}
}
}
but in razor page(page.cshtml) I can’t use @w("hi") but @this.w("hi") works. I want to know how it is possible to this.method() works but method() doesn’t work?
To call a static method you have to specify which class the method belongs to.
In your case, because this is an extension method
thisis referring to the current page which is of typeWebPageBase. Sothis.w("hi")is effectively the same as you are writing:… which is equivalent to: