如何调用与现有方法同名的扩展方法? [英] How to call extension method which has the same name as an existing method?
本文介绍了如何调用与现有方法同名的扩展方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的代码
public class TestA
{
public string ColA { get; set; }
public string ColB { get; set; }
public string ColC { get; set; }
public void MethodA()
{
MessageBox.Show("Original A1.");
}
}
static class ExtenstionTest
{
public static void MethodA(this TestA A1)
{
MessageBox.Show("Extended A1.");
}
}
现在,如果我像这样调用MethodA
Now if I call MethodA like
TestA a = new TestA();
a.MethodA();
它将始终调用原始方法。如何调用扩展方法。
It will always call Original method. How can I call the extension method.
推荐答案
您不能将扩展方法称为常规扩展方法。实例方法将覆盖具有相同签名的扩展方法
You can't call the extension method as a normal extension method. The instance method overrides the extension method with the same signature
编辑:
您可以将其称为静态方法方法
You can call it as a static method
ExtensionTest.MethodA(a);
这篇关于如何调用与现有方法同名的扩展方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文