views:

130

answers:

4

I'm in the process of migrating a library that is written in C++ and has a C# wrapper. The C# wrapper (LibWrapper) has a set of classes with namespaces, like:

namespace MyNamespace
   class MyClass
   class MyOtherClass

My new library, LibraryCS contains the same namespaces and class names as LibWrapper (per user requirement), so I also have:

namespace MyNamespace
   class MyClass
   class MyOtherClass

Now that the migration is done, I'm in the process of creating a test that compares the results of using both libraries, to validate the migration. However, when I try to reference MyNamespace.MyClass I get a compiler error (expectedly!) that says "MyNamespace.MyClass is defined in both LibWrapper and LibraryCS".

Is there any trick around this issue, that will allow me to use two classes with the exact same name but from different assemblies in the same client code?

Alternatively, is there any other way to test this?

Renaming the migrated namespace to something like MyNamespace2 will of course work, but we were asked not to do it, in order to keep the client code easier to migrate.

+4  A: 

In order to load both of these classes in the same executable, you could to load them in a separate Application Domain. This would let you test the assembly, then fully unload it and load the second one and test it.

For details on how to do this, see How to: Load Assemblies into an Application Domain and Unload an Application Domain.

Reed Copsey
Thank you! I'll try this solution.
pgb
A: 

You could load the first assembly at runtime then use reflection to instantiate it and execute it's method. Then unload that assembly, load the second assembly and use reflection to create it and run its methods.

It'd probably be easier to use 2 separate processes then compare the resulting output...

Arnshea
You can't unload an assembly directly, only an AppDomain. In order to do this, you need a separate appdomain with that assembly.
Reed Copsey
Good call, would have to take down the AppDomain to unload the assembly, I'll edit the response.
Arnshea
+9  A: 

You can use an extern alias to reference types with the same fully qualified name from different assemblies. Select the reference to LibraryCS and update Aliases in the properties page from "global" to "LibraryCS", and add extern alias LibraryCS; to the top of your source file, and then you can use LibraryCS::MyNamespace.MyClass to refer to the class in LibraryCS. You can use MyNamespace.MyClass or global::MyNamespace.MyClass to refer to the class in LibWrapper, or you can use an alias for that reference as well.

Quartermeister
Sounds like what I need. I'll give it a shot, thank you!
pgb
+1  A: 

I agree with Quartermeister. See below for more reference: http://blogs.msdn.com/b/ansonh/archive/2006/09/27/774692.aspx

Kari