或者你可以use the mono CLR embedder
托管代码可以通过两种方式调用非托管代码,[使用 P/Invoke 或]使用low-level Mono embedding API。
这很像在 C/C++ 可执行文件中嵌入 Perl、Python 或 Ruby“解释器”(实际上是虚拟机)的老式嵌入。我认为实际上(还)没有 Swig(++) 包装器生成器之类的东西,但这里有一个对 CIL 代码的调用的 sn-p:
class MyClass {
static void Foo (int value) {
...
}
int Bar (string name) {
...
}
}
假设您在 foo_method 和 bar_method 中获得了相应的 MonoMethod*,并且 this_arg 是 MyClass 类型的 MonoObject*,您只需执行:
/* we execute methods that take one argument */
void *args [1];
int val = 10;
/* Note we put the address of the value type in the args array */
args [0] = &val;
/* execute Foo (10);
* it's a static method, so use NULL as the second argument.
*/
mono_runtime_invoke (foo_method, NULL, args, NULL);
/* a string is a reference, so we put it directly in the args array */
args [0] = mono_string_new (domain, "Hello");
/* execute my_class_instance.Bar ("Hello");
* See the Creating Objects section to learn how to get this_arg.
*/
MonoObject *result = mono_runtime_invoke (bar_method, this_arg, args, NULL);
/* we always get a MonoObject* from mono_runtime_invoke (), so to get
* the integer value we need to unbox (which returns a pointer to
* the value stored in the object) and dereference.
*/
int int_result = *(int*)mono_object_unbox (result);
为了额外的娱乐价值:如果您对所有 CIL 代码进行 AOT 编译,您将能够将您的程序集静态链接到您的本机二进制文件(有效地做托管 C++ (c++-cli) 称为混合模式程序集的内容)。看看
mono --aot=static myassembly.dll
和
mkbundle