20

I've imported 2 dlls to my application (third party) Now both of them have a namespace with same name. For example A.B and in both of them there is a class again with a same name. Now I want to create an instance of one of them, but because the namespace and class names are same, the compiler goes ambiguous. How can I specify witch dll used in the place?

Mehrdad
  • 2,054
  • 3
  • 20
  • 34

2 Answers2

37

Let's suppose that you have 2 assemblies (ClassLibrary1.dll and ClassLibrary2.dll) that both define the same class in the same namespace:

namespace Foo
{
    public class Bar
    {
    }
}

Now in the consuming project you could define an additional alias in the references of the class library:

enter image description here

And now you could do the following to help the compiler disambiguate:

extern alias lib1;
extern alias lib2;

class Program
{
    static void Main()
    {
        var barFromLib1 = new lib1::Foo.Bar();
        var barFromLib2 = new lib2::Foo.Bar();
    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • This was similar to a problem I was having but in my case, it was my own code (a small helper class) that I needed to use in 2 different DLLs that were in turn being used by an EXE. Darin's solution worked great! Thanks. – Mick Bruno Oct 29 '12 at 23:18
-1

Just a little improvment or enhanced information: If you have multiple usings, the "extern alias lib1;"-line must be the very first of those usings (But also MS VS informs you about that).

TheBastard
  • 11
  • 1
  • your answer doesn't provide answer to question.Please read the question carefully – Rohit Poudel Jul 19 '17 at 10:29
  • Unfortunately I can not comment, otherwise I would have prefered that to share my knowledge. But as you might read, my answer is just an "improvement", not quite an answer. – TheBastard Jul 01 '21 at 08:03