3

我正在尝试将单声道嵌入到我正在编写的应用程序中,但 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

但第二个数字(由单声道运行时打印)是随机的。我究竟做错了什么?

4

1 回答 1

4

mono_runtime_invoke() 的参数不正确,它需要是未装箱的指针,因为类型 T 是一个结构。从http://docs.go-mono.com/index.aspx?link=xhtml%3Adeploy%2Fmono-api-methods.html的文档:

obj 是“this”指针,对于静态方法,它应该是 NULL,对于对象实例,它应该是 MonoObject*,对于值类型,它应该是指向值类型的指针。

于 2012-07-20T15:08:31.120 回答