ambiguous class with namespace names in 2 dlls

流过昼夜 提交于 2019-12-03 15:18:18

问题


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?


回答1:


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:

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();
    }
}



回答2:


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).



来源:https://stackoverflow.com/questions/11203813/ambiguous-class-with-namespace-names-in-2-dlls

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!