将 C++/CLI 转换为 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/73879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Translate C++/CLI to C#
提问by Matthew Scouten
I have a small to medium project that is in C++/CLI. I really hate the syntax extensions of C++/CLI and I would prefer to work in C#. Is there a tool that does a decent job of translating one to the other?
我有一个使用 C++/CLI 的中小型项目。我真的很讨厌 C++/CLI 的语法扩展,我更喜欢在 C# 中工作。有没有一种工具可以很好地将一个翻译成另一个?
EDIT:When I said Managed c++ before I apparently meant c++/CLI
编辑:当我说 Managed c++ 之前,我显然是指 c++/CLI
采纳答案by Kevin
You can only translate Managed C++ code (and C++/CLI code) to C# if the C++ code is pure managed. If it is not -- i.e. if there is native code included in the sources -- tools like .NET Reflector won't be able to translate the code for you.
如果 C++ 代码是纯托管的,则只能将托管 C++ 代码(和 C++/CLI 代码)转换为 C#。如果不是——即如果源代码中包含本机代码——像 .NET Reflector 这样的工具将无法为您翻译代码。
If you do have native C++ code mixed in, then I'd recommend trying to move the native code into a separate DLL, replace your calls to DLL functions by easily identifiable stub functions, compile your project as a pure .NET library, then use .NET reflector to de-compile into C# code. Then you can replace the calls to the stub functions by p-invoke calls to your native DLL.
如果您确实混合了本机 C++ 代码,那么我建议尝试将本机代码移动到单独的 DLL 中,用易于识别的存根函数替换对 DLL 函数的调用,将您的项目编译为纯 .NET 库,然后使用.NET 反射器反编译为 C# 代码。然后,您可以通过对本机 DLL 的 p-invoke 调用来替换对存根函数的调用。
Good luck! I feel for you!
祝你好运!我对你有感觉!
回答by Jorge Ferreira
.NET Managed C++ is like a train wreck. But have you looked into C++ CLI? I think Microsoft did a great job in this field to make C++ a first class .NET citizen.
.NET 托管 C++ 就像火车失事。但是你研究过 C++ CLI 吗?我认为微软在这个领域做得很好,让 C++ 成为一流的 .NET 公民。
回答by Rob Walker
Back ~2004 Microsoft did have a tool that would convert managed C++ to C++/CLI ... sort of. We ran it on a couple of projects, but to be honest the amount of work left cleaning up the project was no less than the amount of work it would have been to do the conversion by hand in the first place. I don't think the tool ever made it out into a public release though (maybe for this reason).
回到 2004 年,微软确实有一个工具可以将托管 C++ 转换为 C++/CLI ......有点。我们在几个项目上运行了它,但老实说,清理项目剩下的工作量不低于最初手动转换的工作量。我不认为该工具曾经公开发布过(也许是因为这个原因)。
I don't know which version of Visual Studio you are using, but we have managed C++ code that will not compile with Visual Studio 2005/2008 using the /clr:oldSyntax switch and we still have a relic VS 2003 around for it.
我不知道您使用的是哪个版本的 Visual Studio,但我们已经使用 /clr:oldSyntax 开关管理了无法与 Visual Studio 2005/2008 一起编译的 C++ 代码,而且我们仍然有一个遗留的 VS 2003。
I don't know of any way of going from C++ to C# in a useful way ... you could try round tripping it through reflector :)
我不知道以有用的方式从 C++ 到 C# 的任何方式......你可以尝试通过反射器来回它:)
回答by Vivek
I'm not sure if this will work, but try using .Net Reflectoralong with ReflectionEmitLanguageplug-in. The RelelectionEmitLanguage plug-in claims to convert your assembly to c# code.
我不确定这是否可行,但尝试将.Net Reflector与ReflectionEmitLanguage插件一起使用。RelelectionEmitLanguage 插件声称将您的程序集转换为 c# 代码。
回答by Assaf Lavie
Such projects are often done in c++/cli because C# isn't really an elegant option for the task. e.g. if you have to interface with some native C++ libraries, or do very high performance stuff in low level C. So just make sure whoever chose c++/cli didn't have a good reason to do it before doing the switch.
此类项目通常在 c++/cli 中完成,因为 C# 并不是该任务的真正优雅选择。例如,如果您必须与一些本机 C++ 库交互,或者在低级 C 中执行非常高性能的工作。因此,请确保在切换之前选择 c++/cli 的人没有充分的理由这样做。
Having said that, I'm highly skeptical there's something that does what you ask, for the simple reason that not all C++/cli code is translatable to C# (and probably vice versa too).
话虽如此,我非常怀疑有什么东西可以满足您的要求,原因很简单,并非所有 C++/cli 代码都可以翻译为 C#(反之亦然)。