从 C++ 升级到 C#

发布于 2024-09-28 10:13:06 字数 363 浏览 2 评论 0 原文

我有一个用 C++ 编写的大型应用程序,出于各种原因,我打算用 C# 重写它,我已经完成了大量的 Delphi 到 C#、VB 到 C#,但 C++ 到 C# 我从未做过,尽管我有能力 C++ 我想要这是为了尽可能顺利地进行转换。

我主要想问的是,在这次转换中,有哪些陷阱等待着我,是否有我应该注意的关键领域,或者您可以为我提供任何建议。

这篇文章很好,但是还有什么我应该厌倦的吗?

http://msdn.microsoft.com/en-us/magazine/cc301520.aspx

I have a large application written in C++, For various reasons I am going to rewrite it in C#, I have done plenty of Delphi to C#, VB to C# but C++ to C# I have never done, although I am competent in C++ I want this to be as smooth a conversion as possible.

Mainly what I am asking is what pitfalls await me in this conversion is there any key areas I should be aware of or any advice you can provide me.

This article is quite good, but is there anything else I should be weary of?

http://msdn.microsoft.com/en-us/magazine/cc301520.aspx

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

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

发布评论

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

评论(5

高速公鹿 2024-10-05 10:13:06

主要陷阱 - 不要认为这是升级。这些是不同的语言,在许多地方您将需要完全不同的方法来解决问题。因此,您应该考虑以最少的代码重用来重新实现。

这篇文章很正经。

Main pitfall - do not think it's an upgrade. These are DIFFERENT languages, and in many places you will need complete different approach to the problems. So you should think reimplementation with minimal code reuse.

This article is decent.

2024-10-05 10:13:06

我建议您注意对象的生命周期。

在 C++ 中,当你使用完对象后,你会显式地销毁它们。在 C# (.NET) 中则不需要。对象可能会保留某些重要资源(文件句柄、数据库连接等)。如果这是一个问题,请使用 using 指令。

I'd advice you to pay attention to the objects lifecycle.

In C++ you destroy objects explicitly when you've done with them. In C# (.NET) you don't. It can happen that an object holds on to some important resource (file handle, database connection etc.). If it is an issue, make use of the using directive.

把时间冻结 2024-10-05 10:13:06

您需要翻译代码的精神,而不是代码本身。您需要放弃所有必须在 C++ 中完成的事情,因为这就是 C++ 中的完成方式。好的翻译是一个高度创造性的过程,所以要有创造力。

You need to translate the spirit of the code, but not the code itself. You need to leave behind all the things you had to do in C++ because that was how it was done there. Good translation is highly creative process, so be creative.

﹂绝世的画 2024-10-05 10:13:06

一开始,琴弦的处理对我来说是一个陷阱。虽然我在 Visual C++ 中使用指针,但 C# 中的方法确实有返回值。

dummy = dummy.Replace("a", "b");

The handling of strings was a pitfall for me at the beginning. While I Visual C++ you use pointers the methods in C# have indeed return values.

dummy = dummy.Replace("a", "b");
呆橘 2024-10-05 10:13:06

如果您有 C++ dll 并且想在 C# 项目中使用它们,您可以通过 pinvoke DllImport

当您尝试将非托管代码转换或重写为托管代码时,会有很多差异。这是一个 C++ 到 C# 转换器,它非常适合将您的 c++ 代码转换为 C# ,尽管您不能指望使用它来转换整个项目。

If you have C++ dll and you want to use them in your C# project you can used them by pinvoke and DllImport

There will be lot of differences while you will try to convert or rewrite unmanaged code to managed one. Here is a C++ to C# converter which is quite good for converting your c++ code to C# , though you can not expect to convert the whole project using it.

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