1

I have an internal class A that does a lot of stuff and I have a set of other classes B, D, E that inherit from class A. Of course this won't work as the compiler will complain about a public class not being able to extend an internal one (see this for an explanation).

I don't want A to be public as I don't want my users to inherit from it. I want them to only use B, D, E.

I know I can hide the class from Intellisense using [EditorBrowsable(EditorBrowsableState.Never)] but the users can still access it using reflection :(

Is there a way I can achieve this?

Community
  • 1
  • 1
GETah
  • 20,922
  • 7
  • 61
  • 103
  • Why are classes `B`, `D` and `E` outside of the assembly `A` is in? This sorta conveys that `B,D,E` are _"users"_ or outsiders if you will. So `A` should probably just be public. – gideon Jun 25 '12 at 14:03
  • @gideon It does not matter, they can be either in our out. I just don't want to expose the base class – GETah Jun 25 '12 at 14:04
  • You want to allow inheritance from a class to a select few? – hometoast Jun 25 '12 at 14:04
  • @hometoast Partly yes. I want to only expose a set of known features and I don't want the users to define their own – GETah Jun 25 '12 at 14:05
  • @GETah yes it does, an `internal` class is only visible to classes within the assembly, it is NOT visible to classes outside. From your description seems like `B,D,E` are outside the assembly. – gideon Jun 25 '12 at 14:05
  • Since your changing the signature of the method, your not inheriting the object anymore. Use the new modifier on the method. – Brian Jun 25 '12 at 14:06
  • @gideon Yes, they are outside but the assemblies are friends :) – GETah Jun 25 '12 at 14:06
  • @GETah ah well C# doesn't really have the notion of C++'s `friend` – gideon Jun 25 '12 at 14:07
  • 1
    @GETah: If the assemblies are friends (i.e. marked with the [`InternalsVisibleTo`](http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx) attribute), you should have no problems accessing internal identifiers. – O. R. Mapper Jun 25 '12 at 14:08
  • 1
    @gideon Yes it does :) http://msdn.microsoft.com/en-us/library/0tke9fxk(v=vs.80).aspx – GETah Jun 25 '12 at 14:09
  • @GETah ah thanks for teaching me something new. You should probably put this detail in your question. – gideon Jun 25 '12 at 14:11

1 Answers1

6

Assuming that A is in the assembly A, and B, D and E are in the assembly BDE, you can declare the InternalsVisibleTo attribute on A to make BDE a friend assembly. Like this, B, D and E will be able to see A.

Still, you cannot inherit from A, as superclasses cannot have a lower visibility than subclasses. You can solve this by declaring A public while making all of its constructors internal. This way, only code with internal access to A will be able to derive from A.

O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114
  • @GETah: Note that you can also do fancy things with the `sealed` keyword in this respect. e.g. seal classes `B` and `D`, and do not seal `E` on the class level, but only a few members thereof, to create a subclassable base class for custom classes. – O. R. Mapper Jun 25 '12 at 14:18