我在使用 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/