UPDATE: rewriting this answer since the question has been rewritten.
I do not know a single name for the template that you illustrated.
As you might imagine, the reason why it is illegal to access a field is because we do not know that instance b is an instance of _derived. βsecureβ access means that _derived only allowed access to the protected members of _derived instances ; he is not allowed access to the protected members of "SomeOtherType" instances, which also derives from _base.
Now, if the question really is "is there a way to directly access member x from each derived class through any instance?" then yes. You reject the obvious decision to make it internal. (*) There is another way. Do it:
abstract class B { private B() {} private int x; private class D1 : B { } private class D2 : B { } public static B MakeD1() { return new D1(); } public static B MakeD2() { return new D2(); } }
Now methods B and methods of derived classes D1 and D2 can directly access this.x , but methods of other types cannot do this. There are no other derived types except D1 and D2; cannot be, because the only constructor of B is private. And there cannot be any instances of B that are not D1 or D2, because they are abstract.
(*) Remember that if you make it internal, then you only need to worry about you turning to your member. Including code in a code review if they do something offensive to a member is a perfectly acceptable solution.
source share