鉴于下面的 C# 示例代码,从 C 调用静态方法非常容易Bar.Work
。只需获取 a MonoClass*
,然后MonoMethod*
从类中获取 a 并调用它。
问题是,为了获得MonoClass*
我需要提供 Bar 的命名空间,在这种情况下'sampleApp'
,它作为参数提供给函数。
MonoClass *klass = mono_class_from_name(image, "sampleApp", "Bar");
MonoClass*
问题是如果我不知道Bar
的类命名空间,如何获得in C 。有没有办法从 C 中获取程序集中的类型列表?我在单声道的嵌入样本中找不到任何示例。
C# 示例
namespace sampleApp
{
class Bar {
public static void Work() {
Console.WriteLine("Bar.Work called");
}
}
class Program {
public static void Main(string[] args) {
Console.WriteLine("Program.Main called");
}
}
}
Bar.Work
从 C调用
#include <mono/jit/jit.h>
#include <mono/metadata/mono-config.h>
#include <mono/metadata/metadata.h>
#include <mono/metadata/appdomain.h>
#include <mono/metadata/class.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/image.h>
#include <mono/metadata/object.h>
#include <mono/metadata/debug-helpers.h>
int main(int argc, char **argv) {
mono_config_parse(NULL);
MonoDomain *domain = mono_jit_init_version("app", "v4.0.30319");
MonoAssembly *assembly = mono_domain_assembly_open(domain, "sampleApp.exe");
MonoImage *image = mono_assembly_get_image(assembly);
mono_jit_exec(domain, assembly, argc, argv);
//how can I find Bar class if I don't know that it is under the sampleApp namespace?
MonoClass *klass = mono_class_from_name(image, "sampleApp", "Bar");
MonoMethod *method = mono_class_get_method_from_name(klass, "Work", 0);
void *params[1] = { NULL };
mono_runtime_invoke(method, NULL, params, NULL);
mono_jit_cleanup(domain, assembly);
return 0;
}