Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET - Multiple libraries with the same namespace - referencing

Tags:

Today I am faced with a curious challenge...

This challenge involves two .NET libraries that I need to reference from my application. Both of which I have no control over and have the same namespace within them.

So...

I have foo.dll that contains a Widget class that is located on the Blue.Red.Orange namespace.

I have bar.dll that also contains a Widget class that is also located on the Blue.Red.Orange namespace.

Finally I have my application that needs to reference both foo.dll and bar.dll. This is needed as within my application I need to use the Widget class from foo and also the Widget class from bar

So, the question is how can I manage these references so that I can be certain I am using the correct Widget class?

As mentioned, I have no control over the foo or bar libraries, they are what they are and cannot be changed. I do however have full control over my application.

like image 493
MrEyes Avatar asked Jan 21 '11 14:01

MrEyes


People also ask

Can two files have same namespace?

In an operating system, an example of namespace is a directory. Each name in a directory uniquely identifies one file or subdirectory. As a rule, names in a namespace cannot have more than one meaning; that is, different meanings cannot share the same name in the same namespace.

Can you have multiple classes in a namespace C#?

Two classes with the same name can be created inside 2 different namespaces in a single program. Inside a namespace, no two classes can have the same name. In C#, the full name of the class starts from its namespace name followed by dot(.)

Can a namespace can hold more than one class?

You can have a class in 1 to many files (if you use the "partial" keyword). You can have many classes in a namespace.

What is namespace in .NET framework?

System namespace. The System namespace is the root namespace for fundamental types in . NET. This namespace includes classes that represent the base data types used by all applications, for example, Object (the root of the inheritance hierarchy), Byte, Char, Array, Int32, and String.


2 Answers

You need to use an extern alias - introduced in C# 2.

Anson Horton has a walkthrough on his blog which you may find useful.

like image 54
Jon Skeet Avatar answered Sep 20 '22 17:09

Jon Skeet


I think you have to use extern alias

See http://msdn.microsoft.com/en-us/library/ms173212.aspx

Here's a walkthrough. http://blogs.msdn.com/b/ansonh/archive/2006/09/27/774692.aspx

like image 23
Tom B Avatar answered Sep 17 '22 17:09

Tom B