Errore del compilatore CS1540

Aggiornamento: novembre 2007

Messaggio di errore

Impossibile accedere al membro protetto 'membro' tramite un qualificatore di tipo 'tipo1'. Il qualificatore deve essere di tipo 'tipo2' o derivato da esso.
Cannot access protected member 'member' via a qualifier of type 'type1'; the qualifier must be of type 'type2' (or derived from it)

Anche se una classe derivata può accedere ai membri protetti della propria classe base, non può farlo tramite un'istanza della classe base.

Il seguente codice di esempio genera l'errore 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);
   }
}