コンパイラ エラー CS1540

更新 : 2007 年 11 月

エラー メッセージ

'type1' 型の修飾子を通してプロテクト メンバ 'member' にアクセスすることはできません。修飾子は 'type2' 型、またはそれから派生したものでなければなりません。

派生クラスはその基本クラスのプロテクト メンバにアクセスできますが、基本クラスのインスタンスを通じてアクセスすることはできません。

次の例では 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);
   }
}