MarshalDirectiveException(包装器托管到原生)

时间:2012-08-02 19:15:44

标签: 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(wrapper managed-to-native)CSharpToMonoClass:MonoDelegateToPtr(intptr,int,object,string,int)

1 个答案:

答案 0 :(得分:1)

问题是编组程序不知道如何编组System.Object。

我相信你可以使用IntPtr,并使用以下技巧将System.Object转换为IntPtr:

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

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

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