将 C++/CLI 翻译为 C#

发布于 2024-08-05 12:23:29 字数 29 浏览 4 评论 0原文

如何将小型 C++/CLI 项目转换为 C#

How I can translate small C++/CLI project to c#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

裸钻 2024-08-12 12:23:29

一种迂回的手动方法是编译 C++/CLI 项目并在 Reflector< 中打开输出程序集/a>.反汇编每个类,将反汇编的 IL 转换为 C#,然后保存该代码。

至于自动执行此操作的方法,我想不出任何方法。

话虽如此,您确定您真的想要将您的项目转换为 C# 吗?如果您的 C++/CLI 项目使用任何非托管代码,您将很难想出纯托管的等效代码。如果项目或多或少由纯 CLR 代码组成,并且为了用 C++/CLI 编写而用 C++/CLI 编写,我可以理解想要将其转换为 C#。但如果有理由用 C++/CLI 编写它,您可能希望保持这种方式。

One roundabout, manual way would be to compile your C++/CLI project and open the output assembly in Reflector. Disassemble each class, have it convert the disassembled IL to C#, and save that code off.

As for an automatic way to do it, I can't think of any off the top of my head.

Those things being said, are you sure you really want to convert your project to C#? If your C++/CLI project uses any unmanaged code, you'll have a difficult time coming up with a purely managed equivalent. If the project is more or less composed of pure CLR code, and it was written in C++/CLI for the sake of being written in C++/CLI, I can understand wanting to convert it to C#. But if there was a reason for writing it in C++/CLI, you may want to keep it that way.

各空 2024-08-12 12:23:29

恕我直言,逐行是最好的方法。我已经将几个 C++ 风格的项目移植到托管语言中,并且尝试了各种方法;翻译人员、逐行、脚本编写等……随着时间的推移,我发现最有效的方法是逐行执行,尽管一开始这似乎是最慢的方法。

译者丢失了太多东西。没有一个翻译器是完美的,您最终会花费大量时间来修复翻译后的代码。此外,翻译后的代码通常很丑陋,而且往往比手工编写的代码可读性较差。所以结果是一个固定的、不是很漂亮的代码库。

我逐行提供的一些提示

  • 首先定义所有叶类型
  • 对于具有重要(释放内存)析构函数的每种类型,实现 IDisposable
  • 打开 FxCop 规则,检查是否缺少 Dispose 调用以捕获所有内容的地方使用了基于堆栈的 RAII 并且错过了它
  • 非常密切地关注 C++ 中 byref 的使用。

IMHO, line by line is the best way. I've ported several C++ style projects to a managed language and I've tried various approaches; translators, line by line, scripting, etc ... Over time I've found the most effective way is to do it line by line even though it seems like the slowest way at first.

Too much is lost in a translator. No translator is perfect and you end up spending a lot of time fixing up the translated code. Also, translated code as a rule is ugly and tends to be less readable than hand crafted code. So the result is a fixed up, not very pretty code base.

A couple of tips I have on line by line

  • Start by defining all of the leaf types
  • For every type that has a non-trivial (freeing memory) destructor, implement IDisposable
  • Turn on the FxCop rule that checks for lack of Dispose calls to catch all of the places use used stack based RAII and missed it
  • Pay very close attention to the uses of byref in C++.
相思碎 2024-08-12 12:23:29

我还没有尝试过,但我只是用谷歌搜索了一下,发现了这个: http://code2code.net/
根据它,您不应该完全依赖它生成的代码:

您承认此页面只完成了一半的工作。
您需要做进一步的工作。在大多数情况下,翻译后的代码甚至无法编译。

另请阅读:将 C++/CLI 转换为 C#

I haven't tried it, but I just googled it and found this: http://code2code.net/
According t it, you shouldn't fully rely on the code it produces:

You accept that this page does only half the work.
Futher work on your part is required. In most cases, the translated code will not even compile.

Also, read this: Translate C++/CLI to C#

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文