本地C ++和C#互操作 [英] Native C++ and C# interop

查看:232
本文介绍了本地C ++和C#互操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我架构,做一定C ++的工作,但MFC / ATL是太乱了,我喜欢的应用程序,所以我不得不做所有的思想code本机C这个绝妙的主意++和所有的pretty的UI code在C#。但问题是他们两个人之间的互操作性。以前我太忘乎所以这一点,我在想,如果这是一个解决问题,有一个非常好的办法做到这一点。请注意,我不想混逻辑和显示同一模块,因为它会引起烦人高耦合。

So I'm architecting an application that does necessarily C++ work, but MFC/ATL is too messy for my liking, so I had this brilliant idea of doing all the "thinking" code in native C++ and all the pretty UI code in C#. The problem, though, is interoperability between the two of them. Before I get too carried away with this, I was wondering if this is a solved problem, and there's a really good way to do this. Note that I don't want to mix logic and display in the same module, as it gives rise to annoyingly high coupling.

这是我到目前为止有:

告诉我,能不能做到更好呢?

So tell me, can it be done better?

推荐答案

来处理最简单的方法是使用C ++ / CLI,和你的逻辑暴露为.NET类型。

The easiest way to handle this is to use C++/CLI, and expose your logic as .NET types.

这是很容易在引用类包装一个本地C ++类这是直接usuable从C#用户界面。

It's very easy to wrap a native C++ class in a ref class that's usuable directly from a C# user interface.

话虽这么说 - 这是我的计划,原来,在我目前的项目。我的想法是,我需要在本地code对于一些繁重的数学工作,我们通常做的。然而,我发现,它一直更容易,更快,更好,只是将我的大部分逻辑直接到C#(从UI code分开,但还是在C#程序集),而不是试图在实现它C ++。

That being said - this was my plan, originally, in my current project. My thinking was that I'd need the native code for some of the heavy math work we typically do. I've found, however, that it's been easier, faster, and nicer to just move most of my logic directly into C# (separated from the UI code, but still in a C# assembly) rather than try to implement it in C++.

我的经验是,速度已经不是问题 - 不安全的C#code具有几乎总是设法尽可能快或比equivelent C ++调整时更快,更容易来分析和调整的C#$ C $ ℃。

My experience has been that speed has not been an issue - unsafe C# code has nearly always managed to be as fast or faster than the equivelent C++ when tuned, and it's easier to profile and tune the C# code.

这篇关于本地C ++和C#互操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆