views:

90

answers:

2

Is it possible to have a single class reside within two name-spaces and how can I do this?

To clarify: We have a class library (let say root namespace is classLib1), which has grown over time (more classes) and I want to logically group classes into different namespaces. However some of the older classes need to be grouped into these new namespaces (e.g classLib1.section1) and doing so will break legacy code in other assemblys that use this class library. So I want to be able to refer to a class using both name-spaces until we can phase the old ones out.

I can't find any information on this, which suggests there is a reason that people would not want to do this!?!

+7  A: 

No, there is no way to give a single class two names (the namespace is actually just a part of the class name).

As a workaround, you could move the classes to their new location, and create thin wrappers around them at the old location (Facade Pattern). A better solution, of course, would be to move the classes and fix the legacy code accordingly.

dtb
...would suggest why I can't find any info on this - is this to do with how name-spaces compile to CLR ?
Mr Shoubs
Damn you, beat me to it :)
annakata
I had thought of creating a façade layer, but was seeing if there was an easier work around.
Mr Shoubs
A: 

As a work around you can 'using' the class into the second namespace when you need it:

namespace classLib1.section2
{
    public myBigClass
    {

and in every file which uses it in the old namespace you can add one line

namespace classLib1.section1
{
    using myBigClass = classLib1.section2.myBigClass;

as a temporary patch-up until you've fixed this properly.

Rup