是否有可能嵌入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);
}
}
我一直试图利用视觉数小时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屋!