我正在尝试将单声道嵌入到我正在编写的应用程序中,但 mono_field_set_value 的行为不像我认为的那样。这是一个例子:
单测.c:
#include <mono/jit/jit.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/debug-helpers.h>
#include <stdio.h>
#define TRUE 1
int main(){
MonoDomain *domain;
MonoAssembly *assembly;
MonoImage *image;
MonoClass *klass;
MonoObject *object;
MonoClassField *field;
MonoMethodDesc *desc;
MonoMethod *method;
int val;
domain = mono_jit_init ("Domain");
assembly = mono_domain_assembly_open (domain, "T.DLL");;
image = mono_assembly_get_image(assembly);
klass = mono_class_from_name(image, "", "T");
mono_class_init (klass);
object = mono_object_new (domain, klass);
val=11;
field = mono_class_get_field_from_name(klass,"i");
mono_field_set_value(object,field,&val);
//mono_field_set_value((MonoObject*)((void*)object-8),field,&val);
printf("val = %d\n", val);
desc = mono_method_desc_new (":Write()",TRUE);
method = mono_method_desc_search_in_class (desc, klass);
mono_runtime_invoke(method,object,NULL,NULL);
return 0;
}
T.cs
public struct T
{
public int i;
public void Write()
{
System.Console.WriteLine(i);
}
}
运行时,我期望输出:
val = 11
11
但第二个数字(由单声道运行时打印)是随机的。我究竟做错了什么?