为什么需要“这个”关键字,从扩展的类中调用扩展方法 [英] Why is the 'this' keyword required to call an extension method from within the extended class

查看:119
本文介绍了为什么需要“这个”关键字,从扩展的类中调用扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个扩展方法为ASP.NET MVC的ViewPage,例如:

I have created an extension method for an ASP.NET MVC ViewPage, e.g:

public static class ViewExtensions
{
    public static string Method<T>(this ViewPage<T> page) where T : class
    {
        return "something";
    }
}

名称为方法并不在当前存在:

当从视图中调用这个方法,我得到错误的 CS0103(从的ViewPage 导出)背景的除非我使用这个关键字来调用它:

When calling this method from a View (deriving from ViewPage), I get the error "CS0103: The name 'Method' does not exist in the current context" unless I use the this keyword to call it:

<%: Method() %> <!-- gives error CS0103 -->
<%: this.Method() %> <!-- works -->

为什么需要此关键字?或者它没有它的工作,但我失去了一些东西?

Why is the this keyword required? Or does it work without it, but I'm missing something?

(我想一定有这个问题的一个副本,但我没能找到一个)

(I think there must be a duplicate of this question, but I was not able find one)

更新

作为<一个href=\"http://stackoverflow.com/questions/3510964/why-is-the-this-keyword-required-to-call-an-extension-method/3511011#3511011\">Ben罗宾逊说的语法来调用扩展方法只是编译糖。那么,为什么不能编译器会自动检查当前类型的基本类型的扩展方法,而不需要这个关键字?

As Ben Robinson says, the syntax to call extension methods is just compiler sugar. Then why can't the compiler automatically check the for extension methods of the current type's base types without requiring the this keyword?

推荐答案

一对夫妇点:

首先,建议的功能(隐含的这一点。在一个扩展方法调用)是的不必要的的。扩展方法是必要的LINQ查询COM prehensions工作,我们想要的方式;接收机总是在查询指出,所以它不是必要支持隐含这使LINQ工作。

First off, the proposed feature (implicit "this." on an extension method call) is unnecessary. Extension methods were necessary for LINQ query comprehensions to work the way we wanted; the receiver is always stated in the query so it is not necessary to support implicit this to make LINQ work.

二,特性作品反对的扩展方法更一般的设计:即,扩展方法的允许您扩展类型不能扩展自己的,或者是因为它是一个界面,你不知道的实施,还是因为你知道的实施,但没有源$ C ​​$ C。

Second, the feature works against the more general design of extension methods: namely, that extension methods allow you to extend a type that you cannot extend yourself, either because it is an interface and you don't know the implementation, or because you do know the implementation but do not have the source code.

如果您在场景,你正在使用的扩展方法该类型中的一个类型的 的,那么你的的访问源$ C ​​$ C。的你为什么要摆在首位使用扩展方法呢?的你可以,如果你有机会获得扩展类型的源$ C ​​$ C写的实例方法的自己,然后你不必在所有使用扩展方法!那么你的实现可能需要有访问对象的私有状态的优势,其中扩展方法不能。

If you are in the scenario where you are using an extension method for a type within that type then you do have access to the source code. Why are you using an extension method in the first place then? You can write an instance method yourself if you have access to the source code of the extended type, and then you don't have to use an extension method at all! Your implementation can then take advantage of having access to the private state of the object, which extension methods cannot.

使其更容易使用扩展方法从您可以访问被鼓励使用的扩展方法在实例方法类型之内。扩展方法是伟大的,但它通常是更好,如果你有一个使用实例方法。

Making it easier to use extension methods from within a type that you have access to is encouraging the use of extension methods over instance methods. Extension methods are great, but it is usually better to use an instance method if you have one.

鉴于这两点,负担不再落在了语言的设计者解释为何功能执行的的存在。现在落在你解释为什么它的的。功能具有关联巨大的代价。此功能是没有必要的,对作品的扩展方法规定的设计目标;我们为什么要采取实现它的成本是多少?解释什么引人注目的,重要的情况下被启用此功能,我们会考虑在将来实现它。我不认为这证明它的任何引人注目的,重要的场景,但也许有一个,我已经错过了。

Given those two points, the burden no longer falls on the language designer to explain why the feature does not exist. It now falls on you to explain why it should. Features have enormous costs associated with them. This feature is not necessary and works against the stated design goals of extension methods; why should we take on the cost of implementing it? Explain what compelling, important scenario is enabled by this feature and we'll consider implementing it in the future. I don't see any compelling, important scenario that justifies it, but perhaps there is one that I've missed.

这篇关于为什么需要“这个”关键字,从扩展的类中调用扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆