views:

64

answers:

1

I created a assembly having a child class that derives from a parent defined in another assembly.

When I add reference to the child, Visula studio also requires reference to be added to the parent.

Why is it so and how can I prevent it without losing any functionality?

A: 

In C/C++, class definition is present in a .h header file. That gives you ability to reference information about a class (as needed e.g. when you want to inherit from that class) without the need to source file with implementation information. The downside is code duplication (implementation in .cpp file needs to repeat most of the information in .h file).

In .NET world the design is different: an assembly contains both the code for the class (CLR bytecode) as well as all the metadata (class name, information about its members etc.) needed to e.g. inherit from that class.

A consequence of that design is that in order to use a class defined in assembly A that inherits from a class in assembly B, .NET needs both A and B assemblies. Or more generically: if you use anything from a given assembly (a class, an enum, a struct), either directly or indirectly, you need to reference that assembly.

I'm not sure what you want to prevent. If you decide to split your code in two assemblies like you described, there's no way around the need to reference both of them.

There are, of course, different ways of structuring your code but not knowing what goal you're trying to achieve by splitting the code into 2 assemblies in the first place, it's impossible to make a useful suggestion.

Krzysztof Kowalczyk
The objective was to create a wrapper which would shield end users from changes to the underlying layer. This wrapper would reference the base. So if the user has to reference the base then he's not shielded fully. I understand how .Net assemblies are built but I was wondering if there is a way to bypass this (one crazy idea was to swap base class at runtime thru reflection, not sure if that's possible).
Mrchief