Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ambiguous class with namespace names in 2 dlls

Tags:

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?

like image 246
Mehrdad Avatar asked Jun 26 '12 08:06

Mehrdad


People also ask

Can two files have same namespace?

Yes you can surely move ahead with this idea. If we use same namespace name in different files those get automatically clubbed into one. Save this answer.

How do you remove ambiguous reference in C#?

Solution 1 You can: 1) Remove the using statement for the one you are not interested in. 2) Fully qualify the object when you reference it, by prefixing "File" with either "Scripting." or "System.IO."


1 Answers

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();
    }
}
like image 130
Darin Dimitrov Avatar answered Sep 30 '22 02:09

Darin Dimitrov