有人可以向我解释为什么在接下来的第三次调用 DoSomething 时无效吗? (错误消息是“名称‘DoSomething’在当前上下文中不存在”)
public class A { }
public class B : A
{
public void WhyNotDirect()
{
var a = new A();
a.DoSomething(); // OK
this.DoSomething(); // OK
DoSomething(); // ?? Why Not
}
}
public static class A_Ext
{
public static void DoSomething(this A a)
{
Console.WriteLine("OK");
}
}
最佳答案
可以像调用其他静态 方法一样调用扩展方法。
将其更改为 A_Ext.DoSomething(this)
。
如果您问为什么不在 this
上隐式调用它,答案是规范就是这样编写的。我认为原因是在没有限定符的情况下调用它会产生误导。
关于c# - 为什么不能直接调用扩展方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3906170/