Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to limit subclassing of public abstact class to types in same assembly and thus allow protected members typed to internal types
    text
    copied!<p>This question is similar to <strong>c# internal abstract class, how to hide usage outside</strong> but my motiviation is different. Here is the scenario</p> <p>I started with the following:</p> <pre><code>internal class InternalTypeA {...} public class PublicClass { private readonly InternalTypeA _fieldA; ... } </code></pre> <p>The above compiles fine. But then I decided that I should extract a base class and tried to write the following:</p> <pre><code>public abstract class PublicBaseClass { protected readonly InternalTypeA _fieldA; ... } </code></pre> <p>And thus the problem, the protected member is visible outside the assembly but is of an internal type, so it won't compile.</p> <p>The issue at hand is how to I (or can I?) tell the compiler that only public classes in the same assembly as PublicBaseClass may inherit from it and therefore _fieldA will not be expossed outside of the assembly?</p> <p>Or is there another way to do what I want to do, have a public super class and a set of public base classes that are all in the same assembly and use internal types from that assembly in their common ("protected") code?</p> <p>The only idea I have had so far is the following:</p> <pre><code>public abstract class PublicBaseClass { private readonly InternalTypeA _fieldA; protected object FieldA { get { return _fieldA; } } ... } public class SubClass1 : PublicBaseClass { private InternalTypeA _fieldA { get { return (InternalTypeA)FieldA; } } } public class SubClass2 : PublicBaseClass { private InternalTypeA _fieldA { get { return (InternalTypeA)FieldA; } } } </code></pre> <p>But that is UGLY!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload