简体   繁体   English

C#如何使用同一命名空间的两个不同版本(即Kinect 1.8和Kinect 2.0)

[英]C# How to use two different versions of the same namespace (i.e. Kinect 1.8 and Kinect 2.0)

In C#, I want to reference Microsoft.Kinect (v1.8) and Microsoft.Kinect (v2.0) in the same project. 在C#中,我想在同一项目中引用Microsoft.Kinect(v1.8) Microsoft.Kinect(v2.0)。 They are different DLLs but use the same namespace. 它们是不同的DLL,但使用相同的名称空间。 I am able to reference them both with a renaming hack in the .csproj file (by calling them "Microsoft.Kinectv1" a "Microsoft.Kinectv2" respectively. 我可以通过重命名.csproj文件中的引用来引用它们(通过分别将它们分别称为“ Microsoft.Kinectv1”和“ Microsoft.Kinectv2”)。

The problem still remains of how to specifically reference one or the other in code though, since they both have the same namespace. 然而,问题仍然在于如何在代码中专门引用一个或另一个,因为它们都具有相同的名称空间。 Visual Studio notices that the some class is used in both asseblies (v1.8 and v2.0) but I don't know how to specify one version or the other in code. Visual Studio注意到在两个组件(v1.8和v2.0)中都使用了some类,但是我不知道如何在代码中指定一个版本或另一个版本。 I have read several posts about this but none of these posts went as far as showing specifically how to solve this issue. 我已经阅读了几篇有关此问题的文章,但这些文章都没有具体说明如何解决此问题。 How would this be done? 怎么做?

  1. In the properties window give each assembly a different alias (lets say you named them: assem01 and assem02); 在属性窗口中,为每个程序集指定不同的别名(假设您将它们命名为:assem01和assem02);

  2. At the TOP of the file you want to use the same namespaces write: 在要使用相同名称空间的文件的顶部,编写:

    extern alias assem01; extern别名assem01;

    extern alias assem02; extern别名assem02;

Note: These lines MUST be written before any other code in the file. 注意:这些行必须写在文件中的任何其他代码之前。

  1. Now you can access the same namespace from both assemblies, for example: 现在,您可以从两个程序集中访问相同的名称空间,例如:

    var c1 = new assem01::ns1.ns2.myClass(); var c1 = new assem01 :: ns1.ns2.myClass();

    var c2 = new assem02::ns1.ns2.myClass(); var c2 = new assem02 :: ns1.ns2.myClass();

  2. For convenience, you may wish to give some aliases to the namespaces too, for example: 为了方便起见,您可能还希望为命名空间提供一些别名,例如:

    using assem01_ns2 = assem01::ns1.ns2; 使用assem01_ns2 = assem01 :: ns1.ns2;

    using assem02_ns2 = assem01::ns1.ns2; 使用assem02_ns2 = assem01 :: ns1.ns2;

    var c1 = new assem01_ns2.myClass(); var c1 = new assem01_ns2.myClass();

    var c2 = new assem02_ns2.myClass(); var c2 = new assem02_ns2.myClass();

Hope this was helpful. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM