什么是使用C ++ / CLI在C#中的情况或利弊利弊、情况、CLI

2023-09-03 12:49:39 作者:劳资很爷们

我一直在保持与.NET CLR一段时间,现在,我选择的语言是C#。

I have been keeping up with .NET CLR for awhile now, and my language of choice is C#.

直到最近,我不知道C ++ / CLI可能会产生混合模式能够运行本机的可执行文件和管理code。

Up until recently, I was unaware that C++/CLI could produce "mixed mode" executables capable of running native and managed code.

现在知道这一点,就是我在讨论这个属性,并试图确定何时以及如何这种能力将是有益的。另一家开发商的朋友

Now knowing this, another developer friend of mine were discussing this attribute and trying to determine when and how this ability would be useful.

我把它作为一个给定的本地code具有的的能力的更有效率,比管理code强大,在更多的开发时间为代价的。

I take it as a given that native code has the capability to be more efficient and powerful than managed code, at the expense of additional development time.

在过去,我使出严格本地C ++ code库,并使用互操作来利用我写入机库的功能。

In the past, I resorted to strictly native C++ code libraries and used Interop to make use of the functionality I wrote into the native library.

我可以看到如果不需要额外的库的好处,但我很好奇,什么都利弊/使用C ++ / CLI超过索利用C#托管可执行文件创建,或使用互操作这样的可执行文件调用的利弊纯粹的原生C ++库?

I can see the benefit of not requiring an additional library, but I'm curious as to what all the pros/cons of using C++/CLI over soley managed executable created in C#, or such an executable using Interop to call a purely native C++ library?

(旁注:是的条款互操作/ PInvoke的互换,我d.on't理解的术语之间的区别,只是看到他们用同样的方式。)

(Sidenote: Are the terms Interop/PInvoke interchangeable, as I d.on't understand the difference between the terms, simply seen them used the same way.)

推荐答案

使用C ++ / CLI,您可以创建,从广义上说,三种类型的对象:

With C++/CLI you can create, broadly speaking, three types of objects:

托管类型。这将编译到基本相同的IL与等效的C#。有没有表现的机会在这里。 本机类型。编译为本地code,如果你直接使用C ++。 在混合模式类型。这些编译到管理code,但允许您参考本机类型了。

您可能会想到(3)作为像编写C#code用的PInvoke code来访问本地的东西 - 除了所有的PInvoke的东西为您生成

You might think of (3) as being like writing C# code with PInvoke code to accessing the native stuff - except all the PInvoke stuff is generated for you.

还有更多比的,当然,还有一些注意事项 - 但应该给你它是如何有用的想法

There's more to it than that, of course, as well as some caveats - but that should give you an idea of how it's useful.

在换句话说,它是一个真正的胶水语言。虽然你可以在C ++ / CLI写的完全成熟的应用是比较正常的,以保持管理和本地部分分开,并使用C ++ / CLI弥合两个干净比的PInvoke。

In other words it's really a glue language. While you can write fully fledged apps in C++/CLI it's more normal to keep the managed and native parts separate and use C++/CLI to bridge the two more cleanly than with PInvoke.

另一种常见的用途是扩展和现有的,天然的,C ++ $ C $与.net库调用C碱基。

Another common use is to extend and existing, native, C++ code base with .Net library calls.

只是要小心,你的分区code以及它有时是很微妙的编译纯C ++ code到IL透明!

Just be careful that you partition your code well as it can be quite subtle sometimes in compiling your pure C++ code down to IL transparently!

至于你的旁注:PInvoke的是一种特殊类型的互操作的。互操作进来等多种形式得如COM互操作。其实,更准确的说,PInvoke的是一组的语言功能,使互操作与本地code更容易。

As to your sidenote: PInvoke is a particular type of Interop. Interop comes in other forms too, such as COM Interop. In fact, more accurately, PInvoke is a set of language features that make Interop with native code easier.