单嵌入,从C调用C#泛型方法

时间:2014-03-08 18:15:39

标签: c# c mono mono-embedding

我无法弄清楚如何从C调用泛型方法(静态和非静态)。

最大的罪魁祸首是调用泛型方法基本上没有文档记录,单声道存储库中没有示例,但有人提到这可能在文档中有用:

If you want to invoke generic methods, you must call the method on the "inflated" class, which you can obtain from the mono_object_get_class()


MonoClass *clazz;
MonoMethod *method;

clazz = mono_object_get_class (obj);

/*
 * If there are more Add methods declared, you
 * may use mono_method_desc_search_in_class (clazz, ":Add(T)"),
 * you must substitute ":Add(T)" with the correct type, for example
 * for List<int>, you would use ":Add(int)".
 */
method = mono_class_get_method_from_name (clazz, "Add", 1);
mono_runtime_invoke (method, obj, args, &exception);

不幸的是,这没有用,因为它没有显示完整的示例,无论我做什么(使用mono_object_get_class或不使用),我最终会在调用泛型方法时发出警告,然后崩溃。

* Assertion: should not be reached at marshal.c:4315
SIGABRT stracktrace...

下面是一个示例C#类和调用它的C代码。调用非泛型方法工作正常,但我不知道如何调用泛型方法。我很感激任何有用的提示。

样本CSharp

namespace foo {
  class MainClass {
      public static void Main(string[] args) {
              Console.WriteLine("Main");
      }

      public void GenericMember<T>(T t) {
             Console.WriteLine(t);
      }

      public static void GenericStatic<T>(T t) {
             Console.WriteLine(t);
      }


      public void NonGenericMember(string t) {
             Console.WriteLine(t);
      }

      public static void NonGenericStatic(string t) {
            Console.WriteLine(t);
      } 
  }
}

样本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, "foo.exe");  
   MonoImage *image = mono_assembly_get_image(assembly);  

   mono_jit_exec(domain, assembly, argc, argv);

   MonoClass *klass = mono_class_from_name(image, "foo", "MainClass");
   MonoObject *instance = mono_object_new(domain, klass)

   MonoString *string = mono_string_new(domain, "hello");
   void *params[] = { string };

   //NonGenericStatic call (works)
   MonoMethod *method = mono_class_get_method_from_name(klass, "NonGenericStatic", 1);
   mono_runtime_invoke(method, NULL, params, NULL);

   //NonGenericMember call (works);
   method = mono_class_get_method_from(klass, "NonGenericMember", 1);
   mono_runtime_invoke(method, instance, params, NULL);

   //GenericStatic call (fails)
   method = mono_class_get_method_from_name(klass, "GenericStatic", 1);
   mono_runtime_invoke(method, NULL, params, NULL);

   //GenericMember call (fails)
   method = mono_class_get_method_from_name(klass, "GenericMember", 1);
   mono_runtime_invoke(method, instance, params, NULL);

   mono_jit_cleanup(domain, assembly);

   return 0;
}

1 个答案:

答案 0 :(得分:2)

你无法以这种方式调用泛型方法,遗憾的是文档有点误导。如果您想使用public void GenericMember<T>(T t)参数调用String,则必须找到带有签名public void GenericMember<String>(String t)的方法并在其上使用mono_runtime_invoke

更简单的方法是通过C#实用程序方法使用MethodInfo.MakeGenericMethod方法:

public static IntPtr Utility(MethodInfo method, Type type)
{
  return method.MakeGenericMethod(type).MethodHandle.Value;
}

使用嵌入式Mono API查找Utility方法,使用代表MonoMethod*方法的GenericMethod<T>(T t)和您想要的类型(示例中的字符串)调用它。此调用的返回值为MonoMethod*,代表GenericMethod<String>(String t)。这是您需要调用的方法。

我发了一篇详细的帖子,其中包含源代码here