在扩展类型中访问扩展方法?

Accessing extension method within extended type?

我最近一直在重构一些旧代码,很多方法作为基础类型的扩展方法很有用,例如 doublefloatintstring, 等等。 然而,当我在取得长足进步时,我突然想到了一个想法。有多种方法可以访问扩展方法。让我们创建一个名为 Extensions 的容器 class 并添加一个基本的扩展方法(首先),以及一个虚拟 class 我们将使用一个利用我们的扩展的简单方法调用 Dummy称为 DoIt.

的方法
public static class Extensions {
    public static double ToRadians(this double degrees) => degrees * Math.PI / 180.0;
}
public class Dummy {
    public void DoIt() {
        double radians = Extensions.ToRadians(45.0);
        double degrees = 90.0;
        radians = degrees.ToRadians();
    }
}

这不是最优雅的示例,但它演示了广为人知的扩展方法访问。现在让我们为我们的 Dummy class 创建一个名为 WasteOfTime 的愚蠢且毫无意义的扩展,但在 Extensions class.

我不确定你为什么要这样做,但为了这个问题,让我们做吧!

public static class Extensions {
    ...
    public static void WasteOfTime(this Dummy dummy) { }
    ...
}

好吧,现在我们有了这个应该放在 Dummy class 中的完美小方法。好吧,由于扩展方法对类型的实例起作用,您应该能够从 Dummy class' 称为 DoIt.

的特殊方法调用 WasteOfTime
public void DoIt() {
    ...
    WasteOfTime();
    ...
}

哦,等等,你不能那样做!找不到方法!让我们用 this 关键字限定它!

this.WasteOfTime();

更好,编译成功!有用!这是浪费时间!酷!


重要提示

不会考虑实际这样做,但出于好奇我试了一下。如果您能举一个有效的例子说明为什么这是一个好主意,请随时分享,但我相信这是其中之一; 你在想什么 种实现方式。


问题

既然现代编译器不再需要使用 this 关键字来限定字段、属性和方法,为什么我必须限定扩展方法?

我唯一能想到的是扩展方法在技术上对基础是静态的class。

首先,我会注意到在 this 上调用扩展方法有充分的理由。例如,您可能正在编写一个集合 class 并希望在其中使用 LINQ。没有错。现在,就问题而言:

Why did I have to qualify the extension method since modern compilers remove the need for qualifying fields, properties, and methods with the this keyword?

这不是编译器的现代性问题——而是关于语言规范的内容。来自 ECMA-334,第 12.7.6.3 节:

In a method invocation (§12.6.6.2) 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. If expr or any of the args has compile-time type dynamic, extension methods will not apply.

形式为 Method() 的方法调用 不是 其中一种形式,因为没有 expr.

所以编译器只是遵守规范的规则。至于为什么这样设计,那略有不同。我不知道这方面的细节(虽然我可以看到它们是否在规范的注释版本之一中)但我怀疑 Foo() 可以引用实例方法或静态方法当前 class 或任何基础 classes 中的方法使整个事情变得有点棘手。 (从 C# 6 开始,它也可以是 using static 指令导入的任何方法中的静态方法,请注意...)