编译器错误 CS1540
更新:2007 年 11 月
错误消息
无法通过类型“type1”的限定符访问保护成员“member”;限定符必须是类型“type2”(或者从该类型派生的)
虽然派生的 class 可以访问其基类的受保护成员,但它无法通过基类的实例这样做。
下面的示例生成 CS1540:
// CS1540.cs
public class Base
{
protected void func()
{
}
}
public class Derived : Base
{
public static void test(Base anotherInstance)
// the method declaration could be changed as follows
// public static void test(Derived anotherInstance)
{
anotherInstance.func(); // CS1540
}
}
public class Tester : Derived
{
public static void Main()
{
Base pBase = new Base();
// the allocation could be changed as follows
// Derived pBase = new Derived();
test(pBase);
}
}