ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

扩展方法调用

2019-07-20 11:00:26  阅读:215  来源: 互联网

标签:调用 methods extension 扩展 namespace identifier 方法


原文链接:http://www.cnblogs.com/zhy2002/archive/2008/10/26/1319782.html

In a method invocation (§7.5.5.1) of one of the forms

在下列形式的方法调用中

expr . identifier ( )

expr . identifier ( args )

expr . identifier < typeargs > ( )

expr . identifier < typeargs > ( args )

if the normal processing of the invocation finds no applicable methods, an attempt is made to process the construct as an extension method invocation. The objective is to find the best type-name C, so that the corresponding static method invocation can take place:

如果正常的调用流程没有找到适用的方法,则尝试将语言构造处理为扩展方法调用。目标是找到最佳类型名称C,使得(与方法调用构造)对应的(C上的)静态方法能够进行如下替代:

C . identifier ( expr )

C . identifier ( expr , args )

C . identifier < typeargs > ( expr )

C . identifier < typeargs > ( expr , args )

 

The search for C proceeds as follows:

C的搜索循序如下:

·         Starting with the closest enclosing namespace declaration, continuing with each enclosing namespace declaration, and ending with the containing compilation unit, successive attempts are made to find a candidate set of extension methods:从包含调用构造的最近的命名空间声明开始,依次经过其父命名空间,父命名空间的父命名空间,直到整个编译单元,连续尝试查找候选扩展方法集合:

o        If the given namespace or compilation unit directly contains non-generic type declarations Ci with extension methods Mj that have the name identifier and are accessible and applicable with respect to the desired static method invocation above, then the set of those extension methods is the candidate set.如果给定的命名空间或编译单元直接包含若干非泛型类型声明Ci,并且Ci上的扩展方法集合Mj匹配调用构造,那么这些扩展方法构成一个候选集合。匹配是指:方法名称同被调用方法名称相同;方法可访问;expr静态类型匹配扩展方法的this参数。

o        If namespaces imported by using namespace directives in the given namespace or compilation unit directly contain non-generic type declarations Ci with extension methods Mj that have the name identifier and are accessible and applicable with respect to the desired static method invocation above, then the set of those extension methods is the candidate set.对于给定的命名空间或编译单元,如果通过using指令导入的命名空间中包含若干非泛型类型声明Ci,并且Ci上的多个扩展方法匹配正被解析的调用构造,那么这些扩展方法构成一个候选集合。

 

/*

这一部分描述的是候选扩展方法集合的搜索顺序。从内向外遍历ns直到cu,在处理每个ns/cu时先查找其中定义的类型,在查找导入的类型。目标类型当然不必是在本编译单元中定义的,可以是被引用程序集中的类型。

虽然上面将扩展方法所在类型称为“非泛型类” ,但实际上应该是非泛型静态类。

候选扩展方法集合中的所有元素都始终是同一类型的成员。

这组规则实际上是定义了static class上的扩展方法的优先级。

*/

 

·         If no candidate set is found in any enclosing namespace declaration or compilation unit, a compile-time error occurs. 如果没有发现任何候选集合则产生编译错误。

·         Otherwise, overload resolution is applied to the candidate set as described in (§7.4.3). If no single best method is found, a compile-time error occurs.否则,重载决策将应用于候选集合,如果没有找到最佳匹配,将发生编译时错误。

/*

这句话也是有问题的。如果重载决策失败,将查找下一个候选扩展方法集合,而不是直接发生编译错误。

如果在同一优先级有多个候选集合匹配,则发上调用不明确的编译错误。

*/

·         C is the type within which the best method is declared as an extension method.C是最佳方法的包含类型。

Using C as a target, the method call is then processed as a static method invocation (§7.4.4).把方法调用转换为C的静态方法调用。

The preceding rules mean that instance methods take precedence over extension methods, that extension methods available in inner namespace declarations take precedence over extension methods available in outer namespace declarations, and that extension methods declared directly in a namespace take precedence over extension methods imported into that same namespace with a using namespace directive. For example:上述规则意味着实例方法优先于扩展方法;在内层命名空间的扩展方法优先于外层命名空间中的扩展方法,直接在命名空间中定义的中的扩展方法优先于通过using导入的扩展方法。

 

参考:《C# 3.0 specification》

转载于:https://www.cnblogs.com/zhy2002/archive/2008/10/26/1319782.html

标签:调用,methods,extension,扩展,namespace,identifier,方法
来源: https://blog.csdn.net/weixin_30300225/article/details/96570671

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有