在2 DLL的名称空间的名称暧昧类 [英] ambiguous class with namespace names in 2 dlls

查看:138
本文介绍了在2 DLL的名称空间的名称暧昧类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经导入2 dll文件到我的应用程序(第三方)
现在他们两个都具有相同名称的命名空间。
为例A·B
,并在他们两人有一个相同的名字再一类。
现在我想创建其中的一个实例,但由于空间和类名相同,编译器去暧昧。
我如何指定的地方用女巫的dll?

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?

推荐答案

让我们假设你有2个组件( ClassLibrary1.dll ClassLibrary2.dll ),这两个定义相同的命名空间相同的类:

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 DLL的名称空间的名称暧昧类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆