覆盖重叠方法

Override overlapped method

问题很短,但我没有找到解决方案。

假设我们有 class 层次结构

public abstract class A
{
    public virtual string Print() { return "A"; }
}

public class B : A
{
    public virtual new string Print() { return "B"; }
}

public class C : B
{
    public override string Print() { return "C"; }
} 

是否可以在 C class 中覆盖 A.Print?我试图将其作为显式接口实现来实现:

string A.Print() { return "C"; }

但是这里我得到一个错误:

'A' in explicit interface declaration is not an interface

我认为这根本不可能,但希望收到任何其他信息

我还没试过,但你可以很容易地 -

interface IA
{
  string Print();
}

interface IB
{
  string Print();
}


public abstract class A : IA
{
    IA.Print() { return "A"; }
}

public class B : IB, A
{
    IB.Print() { return "B"; }
}

您不能在 C# 中使用,但您可以在 IL 中使用 .override 关键字覆盖该方法将使用的 vtable 槽。 (注意:方法名Namespace.A.Print并不重要,在方法名中包含完整的类型名只是避免冲突的一种有用方式,就像C#对显式接口实现所做的一样)

.class public auto ansi beforefieldinit Namespace.C
    extends [x]Namespace.B
{
    .method private hidebysig virtual 
        instance string Namespace.A.Print () cil managed 
    {
        .override method instance string [x]Namespace.A::Print()
        .maxstack 1
        ldstr "C"
        ret
    }
}