我想用自己的方法替换.NET或ASP MVC框架中包含的扩展方法.
例
public static string TextBox(this HtmlHelper htmlHelper, string name)
{
...
}
Run Code Online (Sandbox Code Playgroud)
可能吗?我无法使用override或new关键字.
考虑以下代码(它有点长,但希望你可以遵循):
class A
{
}
class B : A
{
}
class C
{
public virtual void Foo(B b)
{
Console.WriteLine("base.Foo(B)");
}
}
class D: C
{
public override void Foo(B b)
{
Console.WriteLine("Foo(B)");
}
public void Foo(A a)
{
Console.WriteLine("Foo(A)");
}
}
class Program
{
public static void Main()
{
B b = new B();
D d = new D ();
d.Foo(b);
}
}
Run Code Online (Sandbox Code Playgroud)
如果你认为这个程序的输出是"Foo(B)"那么你和我在同一条船上:完全错了!事实上,它输出"Foo(A)"
如果我从C类中删除虚方法,那么它按预期工作:"Foo(B)"是输出.
为什么编译器选择带有Awhen 的版本B是派生得更多的类?