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

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

问题描述

我在迁移是用C ++编写,并有一个C#包装出库的过程。 C#的包装( LibWrapper )与命名空间,就像一组类:

 命名空间了myNameSpace
   MyClass类
   类MyOtherClass

我的新库, LibraryCS 包含相同的命名空间和类名的 LibWrapper (每用户要求),因此,我也有:

 命名空间了myNameSpace
   MyClass类
   类MyOtherClass

现在的迁移完成时,我在创建测试,使用这两个库,来验证迁移的结果进行比较的过程。然而,当我尝试引用 MyNamespace.MyClass 我得到的,上面写着MyNamespace.MyClass在两个LibWrapper和LibraryCS定义为一个编译器错误(果然!)。<​​/ p >

有没有解决这个问题的任何把戏,这将允许我使用两个具有完全相同的名称,但在同一个客户端code不同的组件?

另外,还有没有其他的办法来测试这一点?

重命名已迁移的命名空间类似 MyNamespace2 当然会的工作,但我们被要求不能做到这一点,为了保持客户端code更容易迁移。


解决方案

您可以使用的外部别名引用类型与不同的组件相同的完全限定名。选择参考,从全球,以LibraryCSLibraryCS和更新别名的属性页,并添加外部别名LibraryCS; 到源文件的顶部,然后您可以使用 LibraryCS :: MyNamespace.MyClass 来引用类LibraryCS。您可以使用 MyNamespace.MyClass 全球:: MyNamespace.MyClass 来引用类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

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

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?

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.

解决方案

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天全站免登陆