简体   繁体   English

将一些C#代码转换为托管或C ++ / CLI代码

[英]Converting some C# code to managed or C++/CLI code

I have an existing C# application. 我有一个现有的C#应用​​程序。 What I want is to write native code for some part of the c# application for speed improvement while another part using C# app and the other part using C++/CLI to provide interfaces to C# application. 我想要的是为c#应用程序的某些部分编写本机代码以提高速度,而另一部分使用C#app,另一部分使用C ++ / CLI为C#应用程序提供接口。 So for latter part, I need to convert C# code into C++/CLI. 所以对于后一部分,我需要将C#代码转换为C ++ / CLI。

I am wondering if I can convert my C# code to C++/CLI code. 我想知道我是否可以将我的C#代码转换为C ++ / CLI代码。 Maybe converting .net assembly to C++/CLI would be a better idea to get specific code. 将.net程序集转换为C ++ / CLI可能是获取特定代码的更好主意。 I looked into reflector and its addin CppCli. 我看着反射器和它的插件CppCli。 But the addin is not available any more. 但是插件不再可用了。 So I am looking for any way to automate this. 所以我正在寻找任何方法来实现自动化。 I would appreciate any advice. 我很感激任何建议。

Generally speaking C++/CLI will have about the same run-time as C# (both are managed and probably would be converted to the same IL). 一般来说,C ++ / CLI与C#的运行时间大致相同(两者都是托管的,可能会被转换为相同的IL)。 If there is a certain part of your application you need to run faster you can write a C++/CLI layer so your C# app can interop with some native C++. 如果您的应用程序的某个部分需要运行得更快,您可以编写C ++ / CLI层,以便您的C#应用​​程序可以与一些本机C ++互操作。 This will be faster if you write your native C++ appropriately. 如果您正确编写本机C ++,这将更快。

EDIT: As Ben Voigt pointed out, the C++/CLI might be more optimized than the C#, but still not nearly as fast as well-written native C++ since it's still managed. 编辑:正如Ben Voigt指出的那样,C ++ / CLI可能比C#更优化,但仍然没有编写良好的原生C ++那么快,因为它仍然可以管理。

If you're looking to wrap some native code in C++/CLI then a conversion tool probably wouldn't structure it in a useful fashion anyway. 如果您希望在C ++ / CLI中包装一些本机代码,那么转换工具可能无论如何都不会以有用的方式构建它。 You're better off just hand-coding the interop portion. 你最好只手工编码互操作部分。

There are commercial tools out there that do this, but I think you're still better off doing it by hand. 有一些商业工具可以做到这一点,但我认为你手工制作它仍然会更好。

You may not have to convert the code. 您可能不必转换代码。

link.exe, part of the C++ compiler tools, is able to include both C++ and C# code in the same assembly. link.exe是C ++编译器工具的一部分,能够在同一个程序集中包含C ++和C#代码。 The C++ bit can include a mixture of managed and unmanaged code. C ++位可以包含托管代码和非托管代码的混合。 You first have to compile the C# code to a .netmodule with command line switches of the C# compiler (csc.exe), and then you can use link.exe to compile it into an assembly with C++ code. 首先必须使用C#编译器(csc.exe)的命令行开关将C#代码编译为.netmodule,然后可以使用link.exe将其编译为带有C ++代码的程序集。 It's been a while since I've created a mixed language assembly so apologies if the details are not 100%, but search for the above terms and you will find a way to do it. 我已经创建了一个混合语言程序集已经有一段时间了,如果细节不是100%,那么道歉,但搜索上述术语,你会找到一种方法。

I seem to remember that the key part is remembering that the C++ compiler is more advanced than the others and so can consume C#/VB netmodules, but not the other way round. 我似乎记得关键部分是记住C ++编译器比其他编译器更先进,因此可以使用C#/ VB netmodules,但不是相反。 I found that the advantage of compiling into a single assembly rather than one referencing another is that types inside each part of the single assembly can cross-reference each other. 我发现编译成单个程序集而不是引用另一个程序集的优点是单个程序集的每个部分内部的类型可以相互交叉引用。 By having references between two separate assemblies the type awareness relationship has to be hierarchical. 通过在两个单独的程序集之间引用,类型感知关系必须是分层的。

There is also the unsafe keyword in C#, which allows C++-ish style pointers to be used within sections of a C# code file, and turns off array bounds checking. C#中还有unsafe关键字,它允许在C#代码文件的各个部分中使用C ++ - ish样式指针,并关闭数组边界检查。 There are disadvantages of this that may or may not matter depending on your usage scenario. 根据您的使用场景,这可能有问题,也可能无关紧要。

Personally I found that running some calculation-intensive code was 20x quicker in the C++ compiler compared to C#, after I had optimised it using the pointers available in C++, which was worth the effort in my case. 我个人发现在C ++编译器中运行一些计算密集型代码比C#快20倍,之后我使用C ++中提供的指针对其进行了优化,这在我的案例中是值得的。

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

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