1

Even though Iam in a derived class which should get me access to the derived protected members, I get the error

"Cannot access protected method 'BaseMethod' from here"

when trying to call other.BaseMethod();. Can I get around this without having to make BaseMethod public? I also cannot make the method internal, since Base and Derived are in different assemblies.

class Base
{
  protected void BaseMethod() { }
}

class Derived: Base
{
  public void Doit(Base other)
  {
    other.BaseMethod();
  }
}
codymanix
  • 28,510
  • 21
  • 92
  • 151
  • Why does `DoIt` take a parameter of `Base` when `Derived` inherits from `Base`? It might be valid, or you might have misunderstoofd how inheritance works – Jamiec Jul 18 '22 at 09:57
  • You can only access *this*.BaseMethod(). You cannot access someOtherObject.BaseMethod() if BaseMethod is protected. – Gec Jul 18 '22 at 10:09
  • @Gec Not quite true: if `other` is declared as `Derived` then you could access it – Charlieface Jul 18 '22 at 10:44
  • 2
    Does this answer your question? [What's the real reason for preventing protected member access through a base/sibling class?](https://stackoverflow.com/questions/1904782/whats-the-real-reason-for-preventing-protected-member-access-through-a-base-sib) – Charlieface Jul 18 '22 at 10:45
  • @Charlieface I believe you can only access protected methods of *this* object. If you are trying to access protected methods of other derived objects (not *this*) you will get that error. – Gec Jul 19 '22 at 08:09
  • @Gec You can trivially create a pass-through function on `Derived` which accesses its `base`, even if this is declare `private` it can still be called by another object of type `Derived` https://dotnetfiddle.net/wregHy – Charlieface Jul 19 '22 at 08:30
  • @Charlieface your fiddle shows "Compilation error (line 31, col 9): Cannot access protected member 'Base.Foo()' via a qualifier of type 'Base'; the qualifier must be of type 'Derived1' (or derived from it)", at least this is what I am seeing. – Gec Jul 19 '22 at 08:32
  • @Gec Correct, that's the case that *doesn't* work, you can't access *another* class's base, but in `Test2` you *can* access `Test` of another object of type `Derived` – Charlieface Jul 19 '22 at 08:34
  • @Charlieface Ah, OK, I see what you mean now :-). Still, it just proves the point, you can only access protected methods on *this*. You access Foo in Test1 :-), not in Test2 and Test3. Of course, using pass-through, we could provide access to e.g. private members of the same class (not base classes). – Gec Jul 19 '22 at 08:37
  • @Charlieface but yes, a nice trick in the box :) – Gec Jul 19 '22 at 08:51

2 Answers2

1

You can get around this by adding protected internal:

class Base
{
    protected internal void BaseMethod() { }
}

class Derived : Base
{
    public void Doit(Base other)
    {
        other.BaseMethod();
    }
}

However, when inheritance is used then it can be called without any params. Let me show an example:

class Base
{
    protected internal void BaseMethod() { }
}

class Derived : Base
{
    public void Doit()
    {
        BaseMethod();
    }
}
StepUp
  • 36,391
  • 15
  • 88
  • 148
-1

You are using another class, as parameter, not the derived one.

in this case, the method should be public.

but have a look a this How do you unit test private methods?

sancelot
  • 1,905
  • 12
  • 31