如何调用c#(mono,.net)方法,从本地c

如何调用c#(mono,.net)方法,从本地c

问题描述:

是否可以从原生c代码(以及如何操作)调用以托管代码(可能在类或库中)编写的c#方法?

is it possible to call c# methods written in managed code (maybe in a class or a library) from a native c code (and how)?

thx

编辑:使用c#我大多指的是单声道或甚至portable.net和操作系统是Linux

edit: with "c#" i mostly refer to mono or even portable.net and the OS is Linux

您的C代码可以定义函数来注册回调。 C#代码可以P /调用这些函数,并将被管理的代理作为参数传递。编组代码将透明地将这些转换为C函数指针。

Your C code can define functions to register callbacks. The C# code can P/Invoke these functions, and pass managed delegates as arguments. The marshalling code will transparently transform these to C function pointers.

或者,从C侧接近它,您可以使用Mono嵌入API加载程序集,查找MonoMethods并调用它们。

Alternatively, approaching it from the C side, you can use the Mono embedding API to load assemblies, look up MonoMethods, and invoke them.

使用嵌入API要复杂得多。如果您的入口点在C中,您必须使用嵌入API,但是可能更容易简单地写一个托管方法来执行回调注册和任何其他托管设置,那么您只需要加载和调用该单个方法从C代码。

Using the embedding API is much more complicated. If your entry point is in C, you'll have to use the embedding API, but it's likely easier to simply write a managed method to do the callback registration and any other managed setup, then you only have to load and invoke that single method from C code.