22

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
MrEyes
  • 13,059
  • 10
  • 48
  • 68

5 Answers5

45

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

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    Might be worth noting, every assembly has the extern alias of `global` by default – Conrad Frix Jan 21 '11 at 14:20
  • Aliases seems to be an option for different versions of the same assembly, not for different assembly purposes, that´s just wrong architecture. – Edwin Jarvis Jan 21 '11 at 14:26
  • 2
    @Augusto: It works either for different versions of the same assembly, or for different assemblies which include the same namespace-qualified type name. It's not solely for one situation or the other. Are you claiming my solution wouldn't work? If so, do you have anything to back up that claim? – Jon Skeet Jan 21 '11 at 14:28
  • @Jon Skeet: Your solution isn't the problem, but it's a clever "hack" for an architecture problem. He shouldn't have two different classes with same name within same namespaces in different assemblies. For two different implementations of the same concept he should be using an interface. As he doesn't have control over it, I suppose your solution is fine. – Edwin Jarvis Jan 23 '11 at 14:05
  • @Augusto: Don't forget that the libraries may be third party ones, with no individual third party doing anything wrong (although the common case here would probably be inappropriate use of the "global" namespace). – Jon Skeet Jan 23 '11 at 20:23
  • How can I use extern in my Razor or webform views? – Imran Qadir Baksh - Baloch Feb 18 '15 at 12:13
  • @user960567: No idea, I'm afraid. – Jon Skeet Feb 18 '15 at 12:37
  • @JonSkeet walkthrough link is dead – Haighstrom Dec 08 '22 at 17:52
8

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

Tom B
  • 2,160
  • 2
  • 13
  • 15
1

You could use the "Aliases" property for resolving conflicts. Select the library and open Property window. This is already discussed in this SO post

Community
  • 1
  • 1
AbrahamJP
  • 3,425
  • 7
  • 30
  • 39
0

Wow, thats tricky. I tink, I would create two wrapper-DLLs:

  1. MyBarWrapper.dll (which has a reference to bar.dll)
  2. MyFooWrapper.dll (which has a reference to foo.dll)

and support different namespaces there.

Alexander Schmidt
  • 5,631
  • 4
  • 39
  • 79
-1

I'd be surprised if you can do this, without using reflection.

It might be easier to create your own FooWrapper and BarWrapper dlls which reference foo and bar and expose their contents again under different namespaces.

Massif
  • 4,327
  • 23
  • 25