As far as I know, C# only let us use extension methods with an instance of a class:
public class MyClass
{
public static string GetStuff()
{
return string.Empty;
}
}
public static class MyClassExtension
{
public static string GetOtherStuff(this MyClass myClass)
{
return string.Empty;
}
}
Usage:
MyClass.GetStuff();
MyClass.GetOtherStuff(); // Cannot resolve symbol 'GetOtherStuff'
new MyClass().GetOtherStuff(); // This works
However, I noticed that the MVC framework allows me to extend the HtmlHelpers in such a way that I can use my extension methods without creating any instance of a class. For example, if I create an extension method for HtmlHelper
like this:
public static string MyHtmlHelper(this HtmlHelper helper)
{
return string.Empty;
}
I can use it in a Razor View like this:
@Html.MyHtmlHelper() // no need to instantiate a class
I would really like to achieve the same result with my MyClass
, so I can do:
MyClass.GetOtherStuff()
How could I do that?