c# - MarshalDirectiveException at(包装器托管到 native )

标签 c# c mono xamarin.ios wrapper

我在使用 C# 为 iPhone 构建应用程序时发现了此错误。 这个应用程序有一个插件,可以将 System.Object 实例传递给 c。我得到了这个,但我不确定缺少哪一部分!

这是 C# 代码:

public static void Call( System.IntPtr L, System.Delegate pDelegate)
{   
    MonoDelegateToPtr( L, 0, pDelegate.Method.Target, pDelegate.Method.Name, pDelegate.Method.GetParameters().Length);
}

[System.Runtime.InteropServices.DllImport("__Internal")]
static extern void MonoDelegateToPtr( System.IntPtr L, int pN, System.Object pObj, string pMethod, int pParamCount);
<小时/>

这是 C 代码:

extern "C" void MonoDelegateToPtr( lua_State* L, int pN, MonoObject* pObj, const char* pMethod, int pParamCount)
{
   MonoMethod *method;

   MonoObject *pObject;
   method = GetCSMethod( pObject, pMethod, pParamCount);
   lua_CFunction func = (lua_CFunction)[MonoUtility MonoDelToPtr: method];
   if( func==0)
   {
      printf("****ERROR DELEGATE TO FUNCTION PTR IS NULL%s\n", pMethod);
      return;
   }

   lua_pushcclosure( L, func, pN);
}

MonoMethod* GetCSMethod( MonoObject *pObj, const char* pMethod, int pParamsTotal)
{
   MonoClass *class = mono_object_get_class( pObj);
   MonoMethod   *methodDef = mono_class_get_method_from_name(  class, pMethod,   pParamsTotal);

   return mono_object_get_virtual_method((MonoObject*)objectInstance, methodDef);
}

这是错误消息:

MarshalDirectiveException

at(包装器托管到 native )CSharpToMonoClass:MonoDelegateToPtr (intptr,int,object,string,int)

最佳答案

问题在于编码器不知道如何编码 System.Object。

我相信您可以使用 IntPtr 和以下技巧将 System.Object 转换为 IntPtr:

struct ObjWr {
    [FieldOffset (0)] IntPtr ptr;
    [FieldOffset (0)] object obj;
}

然后将对象存储在 obj 字段中,并从 ptr 字段中读回指针。

但是我不完全确定这是否是您想要做的事情的正确方法,但我不清楚您实际上想要做什么,所以也许您可以更好地解释一下?

关于c# - MarshalDirectiveException at(包装器托管到 native ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11784273/

相关文章:

c# - Bing map 路线优化

c# - Iridium ORM - 无法创建关系。 (

c - GCC 中静态变量的符号是如何命名的?

c - 32 位还是 64 位?使用 C 代码

c - 有什么可以更改 GCC 中的导出名称修改方案吗?

c# - 用 Mono 编译 IronPython

c# - 如何保持长时间运行的 NHibernate Session 数据一致?

Mono 和 Windows 上的 F# - 与简单功能的差异

c# - 在 Windows 上使用单声道

c# - 将列表转换为父类列表