473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Prevent inheritance outside of assembly

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?
Apr 12 '06 #1
7 3128
Hi Kyle,

Try declaring your base class as "internal":
http://msdn2.microsoft.com/en-us/lib...1b(VS.80).aspx

HTH,

Chris

Apr 12 '06 #2
In addition to making the base class internal, add the "sealed" keyword
to the public classes that should not be inherited from.

Michael Lang

Apr 12 '06 #3
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."

Apr 12 '06 #4
Bruce Wood <br*******@canada.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 12 '06 #5
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.

"Chris Fulstow" <ch**********@hotmail.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com...
Hi Kyle,

Try declaring your base class as "internal":
http://msdn2.microsoft.com/en-us/lib...1b(VS.80).aspx

HTH,

Chris

Apr 12 '06 #6
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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Bruce Wood <br*******@canada.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Apr 12 '06 #7

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Bruce Wood <br*******@canada.com> wrote:
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 it were, you could add public static "factory" methods to call the
constructors for you.
Apr 12 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: mje11 | last post by:
Hi there I have a class library project (AssemblyA) and a 'test' project grouped under the same solution. AssemblyA is strongly-named and I have a version of the assembly in the GAC. In the...
6
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
0
by: JKJ | last post by:
I'm not an OOP guru, but I would say ". . .not necessarily" What if the base class and derived classes are internal? Then the base class has to be within the same assembly to be inherited from. ...
1
by: Zachary Hartnett | last post by:
I was trying to write a routine this morning that would open a given assembly, walk the inheritance tree of classes in the assembly, and provide a list of classes in the assembly that inherit from...
3
by: Tony Maresca | last post by:
Hi. I have a class derived from a UserControl, that I want to allow others to derive controls from. I don't want them to design the base class (which is derived from a UserControl). I know that...
2
by: Brad | last post by:
I have one of those seemingly simple questions that evades/confuses me. I've created an assembly with bass classes (classes meant to be inherited in other assemblys). In a secondary assembly (my...
33
by: Joe Fallon | last post by:
1. I have a Base class that has a certain amount of functionality. 2. Then I have a CodeSmith generated class that inherits from the Base class and adds functionality. 3. Since I want to be able...
5
by: rkozlin | last post by:
Running into an issue where the compiler will throw an error... "The type '<BaseClass>' is defined in an assembly that is not referenced. You must add a reference to assembly '<BaseClass>'." ...
2
by: Craig Buchanan | last post by:
I have three classes: Core Assembly Application - creates instances of User using Activator.CreateInstance Item - marked MustInherit; has an Init method UserPlugin Assembly User - Inherits...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.