Prevent inheritance outside of assembly

  • Thread starter Thread starter Kyle M. Burns
  • Start date Start date
K

Kyle M. Burns

I have an application where I needed to refactor to a base class using the
template method pattern in order to avoid duplication of code between two
objects that needed to fulfill the same obligation in a different manner.
The classes are publicly visible because the application will be used by
other applications. What I would like to do is prevent objects that exist
outside the assembly from inheriting the base class in order to avoid having
functionality "injected" into the application. The deployment environment
does not offer GAC as an option and the code will be deployed to ASP.Net
applications. Any ideas?
 
In addition to making the base class internal, add the "sealed" keyword
to the public classes that should not be inherited from.

Michael Lang
 
However, if the OP needs to expose the base class as "public," perhaps
because he has methods that return the base class, then he's up the
creek, isn't he?

There is no way that I know of to expose a class as "public" to an
assembly but say, "There are classes inside this assembly that inherit
from this class but nobody outside the assembly may do so... but the
base class is still public."
 
Bruce Wood said:
However, if the OP needs to expose the base class as "public," perhaps
because he has methods that return the base class, then he's up the
creek, isn't he?

There is no way that I know of to expose a class as "public" to an
assembly but say, "There are classes inside this assembly that inherit
from this class but nobody outside the assembly may do so... but the
base class is still public."

There's an easy way - make all constructors internal. As any derived
classes would have to implicitly call the constructor in the class, the
only classes which could derive from it would be the ones which can
call the constructors.

The only problem that doesn't solve is the one where you need to be
able to call the constructor from outside the assembly. Hopefully
that's not an issue in this case.
 
If the base class is declared as internal, then all subclasses must be
internal. The scope of visibility for a subclass cannot be greater than
that of its base.
 
You're right. I was misinterpreting the scope of the "internal" protection
level to be that of namespace scope rather than assembly scope. The
internal constructor should do the trick. Thanks for pointing this out.
 
Jon Skeet said:
There's an easy way - make all constructors internal. As any derived
classes would have to implicitly call the constructor in the class, the
only classes which could derive from it would be the ones which can
call the constructors.

The only problem that doesn't solve is the one where you need to be
able to call the constructor from outside the assembly. Hopefully
that's not an issue in this case.

If it were, you could add public static "factory" methods to call the
constructors for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top