是否可以在 C# 项目中嵌入 C 代码? [英] Is it possible to embed C code in a C# project?
问题描述
我知道可以将我的 C 代码编译成 dll,然后使用 P/Invoke 调用该代码.
I know that it's possible to compile my C code into a dll, and then use P/Invoke to call that code.
我想知道是否有可能在我的代码中直接嵌入一大块 C 代码,也许只对一个类可用...
What I wondered if it was possible to have a chunk of C code embedded directly in my code, perhaps only available to one class...
类似这样的(非工作)示例:
Something like this (non-working) example:
public class MyClass {
extern "C" {
int do_something_in_c(int i) {
return i*2;
}
}
public int DoSomething(int value) {
return do_something_in_c(value);
}
}
我已经使用 Visual Studio 2008 尝试了几个小时,但我一无所获,我怀疑这实际上是不可能的.任何人都可以确认或否认这一点吗?
I've been trying for a few hours using Visual Studio 2008, but I'm not getting anywhere, and I suspect that it isn't actually possible. Can anyone confirm or deny this?
谢谢.
推荐答案
可以创建混合模式程序集(即同时具有托管代码和本机代码的程序集),但只有 C++/CLI 编译器可以生成其中之一.C# 编译器不支持您要执行的操作.
It is possible to create a mixed-mode assembly (that is, one that has both managed and native code), but only the C++/CLI compiler can produce one of these. What you're looking to do is not supported by the C# compiler.
这篇关于是否可以在 C# 项目中嵌入 C 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!