覆盖重叠方法

Ale*_*kiy 5 .net c# cil

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

假设我们有类层次结构

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"; }
} 
Run Code Online (Sandbox Code Playgroud)

是否有可能在C类中覆盖A.Print?我尝试将其作为显式接口实现:

string A.Print() { return "C"; }
Run Code Online (Sandbox Code Playgroud)

但在这里我收到一个错误:

显式接口声明中的"A"不是接口

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

Bri*_*hle 5

你不能在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
    }
}
Run Code Online (Sandbox Code Playgroud)