没有这个的Controller扩展方法

Tom*_*mas 4 c# asp.net-mvc-3

我想创建控制器扩展方法.到目前为止我得到的是下面的内容

public ActionResult Foo()
{
    this.ExtensionMethod();
    return View();
}

public static void ExtensionMethod(this Controller controller)
{

}
Run Code Online (Sandbox Code Playgroud)

我不喜欢的是必须使用this关键字调用ExtensionMethod .有可能摆脱this

sho*_*nik 8

没有.

它是this使该方法的扩展方法关键字.没有它,它只是一种静态方法.

编辑:对不起,我误解了这个问题.有两个this关键字:一个在扩展方法中,一个用于调用它.

this调用它时需要关键字的原因是您需要指定要扩展的对象.除非指定this关键字,否则C#不会自动解析对扩展方法的本地方法调用.