为什么编译器会给出错误"名称'标识符'在当前的背景下不存在"?

时间:2016-09-26 21:25:18

标签: c# generics

这里是上下文(C#)。

public class Base
{
    public void SomeMethod()
    {

    }
}

public class Intermediate<TIntermediateTypeParam>
{

}

public class Derived<TDerivedTypeParam> : Intermediate<string>
{
    public void SomethingElse()
    {
        SomeMethod(); // Error: The name 'SomeMethod' does not exist in the current context.
    }
}

这是出乎意料的。 关于为什么以及如何解决它的任何解释?

1 个答案:

答案 0 :(得分:1)

Intermediate不是方法所在的Base的子类,因此它不属于Derived类,Derived无法直接访问它。

您需要从Base的实例调用它,或者使用Intermediate(或Derived)子类Base