为什么我需要这个。在母版页中使用扩展方法的限定符?
我在某些母版页代码后面有以下代码行,但如果没有 this.
则失败,这是为什么?
Repeater rep = this.FindControlsByIdRegEx("maintTableRepeater")[0] as Repeater;
这是在母版页Load事件中,扩展方法定义为;
public static List<Control> FindControlsByIdRegEx(this Control control, string idPattern)
I have the following line of code in some master page code behind, but it fails without the this.
Why is that?
Repeater rep = this.FindControlsByIdRegEx("maintTableRepeater")[0] as Repeater;
This is in the master page Load event, and the extension method is defined as;
public static List<Control> FindControlsByIdRegEx(this Control control, string idPattern)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
扩展方法必须有一个“挂起”的实例。
没有对象的方法调用始终引用具有该签名的方法 - 在同一实例中。扩展方法不是实例的一部分。它们是存在于系统其他地方的静态方法,并使用一点编译器糖(实际上是一个技巧),看起来像是挂在对象上以便于编码。
Extension methods must have an instance to "hang off of".
A method call without an object always refers to the method with that signature - in the same instance. Extension methods are not part of the instance. They are static methods that live elsewhere in the system, and use a little bit of compiler sugar - a trick, really - to look like they hang off of an object for ease of coding.