I have a protected method in base class :
public class BaseClass
{
protected virtual void Foo(){}
}
The method is overrided one of the derived classes:
public class Derived1 : BaseClass
{
protected override void Foo()
{
//some code...
}
}
Another derived class has an instance of the first derived class.
When I try to access the Foo method (existing in base class, as mentiond) I get an error :
public class DerivedClass2 : BaseClass
{
BaseClass instance = new DerivedClass1();
instance.Foo(); // Here I get an error
}
The error I get:
Error CS1540: Cannot access protected member 'BaseClass.Foo' via a qualifier of type 'BaseClass';
the qualifier must be of type 'DerivedClass2' (or derived from it)
I understand that protected members should not give up their value to any other instance, even an instance derived from the same type,
but is there a way not to modify the method as public?