我有一个用 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
发布评论
评论(5)
主要陷阱 - 不要认为这是升级。这些是不同的语言,在许多地方您将需要完全不同的方法来解决问题。因此,您应该考虑以最少的代码重用来重新实现。
这篇文章很正经。
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.
我建议您注意对象的生命周期。
在 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.您需要翻译代码的精神,而不是代码本身。您需要放弃所有必须在 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.
一开始,琴弦的处理对我来说是一个陷阱。虽然我在 Visual C++ 中使用指针,但 C# 中的方法确实有返回值。
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.
如果您有 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.