編譯器錯誤 CS1540
更新:2007 年 11 月
錯誤訊息
無法經由型別 'type1' 的限定詞來存取保護的成員 'member'; 必須經由型別 'type2' (或從其衍生的型別) 的限定詞
雖然衍生類別可存取其基底類別的 protected 成員,但它不能經由基底類別的執行個體 (Instance) 進行存取。
下列範例會產生 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);
}
}