继承真的隐藏了方法吗?

tel*_*all 1 c# inheritance interface method-hiding

我收到以下编译器警告:

'Resources.Foo.GetType()' hides inherited member 'object.GetType()'. Use the new keyword if hiding was intended.    Resources.NET\Resources\Class1.cs   123 20  Resources
Run Code Online (Sandbox Code Playgroud)

对于这个(非常简化的)代码:

public interface IFoo
{
    int GetType();
    string GetDisplayName();
}

public class Foo : IFoo
{
    public int GetType() { return 3; }   *** Warning: points to here (line 123)***
    public string GetDisplayName() { return "Foo"; }
}
Run Code Online (Sandbox Code Playgroud)

注意,我没有得到的是为什么GetDisplayName()函数没有得到警告.我显然忽略了一些东西.

我已经探讨了Stack Overflow并搜索了这个错误,它似乎适用于类继承,而不是接口.我真的很困惑为什么会为接口触发它(它将其方法定义为虚拟).

提前感谢您的见解.

CMS*_*CMS 10

所述的GetType方法所定义的System.Object的类,这是最终的基类的框架的所有类.

此方法用于在运行时获取当前实例的Type,我认为您不打算覆盖它(您有一个int返回类型).

我建议你将方法重命名为GetFooType或其他东西,或者显式实现你的接口以避免运行时问题.