我有基类A
public class A
{
public virtual void Method(A parameter)
{
Console.WriteLine(MethodBase.GetCurrentMethod());
}
public virtual void Method(B parameter)
{
Console.WriteLine(MethodBase.GetCurrentMethod());
}
}
继承的B
public class B : A
{
public virtual void Method(object parameter)
{
Console.WriteLine(MethodBase.GetCurrentMethod());
}
public override void Method(A parameter)
{
Console.WriteLine(MethodBase.GetCurrentMethod());
}
public override void Method(B parameter)
{
Console.WriteLine(MethodBase.GetCurrentMethod());
}
}
S
具有扩展方法的静态类
public static class S
{
public static void Method(this B instance, B parameter)
{
Console.WriteLine(MethodBase.GetCurrentMethod());
}
}
示例我们创建类型的实例B
并对其进行调用Method
,我们希望它是public override void Method(B parameter)
实际结果public virtual void Method(object parameter)
。
var b = new B();
b.Method(new B()); // B.Method (Object parameter) Why???
为什么编译器不选择更合适的方法??? UPD为什么它不是扩展方法?