Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 位于的MarshalDirectiveException(包装器管理为本机)_C#_C_Mono_Xamarin.ios_Wrapper - Fatal编程技术网

C# 位于的MarshalDirectiveException(包装器管理为本机)

C# 位于的MarshalDirectiveException(包装器管理为本机),c#,c,mono,xamarin.ios,wrapper,C#,C,Mono,Xamarin.ios,Wrapper,我在使用c#为iPhone构建应用程序时发现了这个错误。 这个应用程序有一个插件,可以将System.Object实例传递给c。我得到了这个插件,我不确定缺少了什么部分 以下是C#代码: 下面是C代码: public static void Call( System.IntPtr L, System.Delegate pDelegate) { MonoDelegateToPtr( L, 0, pDelegate.Method.Target, pDelegate.Method.Na

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

以下是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);
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

在(wrapper managed to 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字段读回指针


但是我不完全确定这是你想做的正确方法,但我不清楚你到底想做什么,所以,也许你可以更好地解释一下?

实际上,我正试图解决Mono iphone中反向回调的问题,为C制作一个包装器,它发送System.Object实例,并将在C中进行操作。还有一个内置的解决方案,它更好,但它只适用于静态方法的委托:使用Marshal.GetFunctionPointerForDelegate并使用MonoPInvokeCallbackAttribute修饰静态方法。