为什么重载方法比实例方法低优先级(Why overloaded methods have lower

2019-07-31 04:15发布

我有基类的A

public class A
{
    public virtual void Method(A parameter)
    {
        Console.WriteLine(MethodBase.GetCurrentMethod());
    }
    public virtual void Method(B parameter)
    {
        Console.WriteLine(MethodBase.GetCurrentMethod());
    }
}

Inhereted 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???

为什么编译器不选择更suitible方法??? UPD为什么它不是扩展方法?

Answer 1:

Why compiler doesn't select more suitible method?

Because it's following the rules of the language specification, where any candidate methods originally declared in a base class (over if they're overridden in a derived class) are ignored when finding a candidate method, unless the derived class doesn't have any applicable methods, at which point the search moves up to the base class, etc.

This is designed to avoid the "brittle base class" problem, but I find that hard to swallow in the face of the method being overridden in the derived class.

The relevant bit of the C# 4 specification is 7.4, which ends with this:

For member lookups in types other than type parameters and interfaces, and member lookups in interfaces that are strictly single-inheritance (each interface in the inheritance chain has exactly zero or one direct base interface), the effect of the lookup rules is simply that derived members hide base members with the same name or signature.

EDIT: About extension methods...

And why it is not extension method?

From section 7.6.5.2 of the spec:

In a method invocation (§7.5.5.1) 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

So an extension method is only used as a last resort, basically.



文章来源: Why overloaded methods have lower priority than instance method