Поделиться через


Ошибка компилятора CS1540

Обновлен: Ноябрь 2007

Сообщение об ошибке

Доступ к защищенному члену "member" через квалификатор типа "type1" невозможен; квалификатор должен иметь тип "type2" (или производный от него тип)
Cannot access protected member 'member' via a qualifier of type 'type1'; the qualifier must be of type 'type2' (or derived from it)

Хотя производный класс может обращаться к защищенным членам своего базового класса, он не может получить к ним доступ с помощью экземпляра базового класса.

В следующем примере возникает ошибка 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);
   }
}