在两个程序集中具有相同名称的类(故意) [英] Class with same name in two assemblies (intentionally)

查看:43
本文介绍了在两个程序集中具有相同名称的类(故意)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在迁移一个用 C++ 编写并具有 C# 包装器的库.C# 包装器 (LibWrapper) 有一组带有命名空间的类,例如:

I'm in the process of migrating a library that is written in C++ and has a C# wrapper. The C# wrapper (LibWrapper) has a set of classes with namespaces, like:

namespace MyNamespace
   class MyClass
   class MyOtherClass

我的新库 LibraryCS 包含与 LibWrapper 相同的命名空间和类名(根据用户要求),所以我也有:

My new library, LibraryCS contains the same namespaces and class names as LibWrapper (per user requirement), so I also have:

namespace MyNamespace
   class MyClass
   class MyOtherClass

现在迁移已经完成,我正在创建一个测试来比较使用这两个库的结果,以验证迁移.但是,当我尝试引用 MyNamespace.MyClass 时,我收到一个编译器错误(预期!),显示MyNamespace.MyClass 在 LibWrapper 和 LibraryCS 中都定义了".

Now that the migration is done, I'm in the process of creating a test that compares the results of using both libraries, to validate the migration. However, when I try to reference MyNamespace.MyClass I get a compiler error (expectedly!) that says "MyNamespace.MyClass is defined in both LibWrapper and LibraryCS".

关于这个问题有什么技巧,可以让我在同一个客户端代码中使用两个具有完全相同名称但来自不同程序集的类?

Is there any trick around this issue, that will allow me to use two classes with the exact same name but from different assemblies in the same client code?

或者,还有其他方法可以测试吗?

Alternatively, is there any other way to test this?

将迁移的命名空间重命名为 MyNamespace2 之类的名称当然可以,但我们被要求不要这样做,以使客户端代码更易于迁移.

Renaming the migrated namespace to something like MyNamespace2 will of course work, but we were asked not to do it, in order to keep the client code easier to migrate.

推荐答案

您可以使用 extern alias 从不同的程序集中引用具有相同完全限定名称的类型.选择对LibraryCS的引用并在属性页中将Aliases从global"更新为LibraryCS",并在你的源文件顶部添加extern alias LibraryCS;,然后就可以使用了LibraryCS::MyNamespace.MyClass 引用 LibraryCS 中的类.您可以使用 MyNamespace.MyClassglobal::MyNamespace.MyClass 来引用 LibWrapper 中的类,或者也可以为该引用使用别名.

You can use an extern alias to reference types with the same fully qualified name from different assemblies. Select the reference to LibraryCS and update Aliases in the properties page from "global" to "LibraryCS", and add extern alias LibraryCS; to the top of your source file, and then you can use LibraryCS::MyNamespace.MyClass to refer to the class in LibraryCS. You can use MyNamespace.MyClass or global::MyNamespace.MyClass to refer to the class in LibWrapper, or you can use an alias for that reference as well.

这篇关于在两个程序集中具有相同名称的类(故意)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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