我在使用 --aot-only 运行时遇到消息 Attempting to JIT compile method '(wrapper managed-to-native) ;...wrapper_aot_native (object)'。
我有一个包含方法指针的结构,应该由 native 函数(称为 LoadContext)初始化,这个结构看起来像这样:
[StructLayout (LayoutKind.Sequential)]
public struct WrapperContext
{
public int ctxVersion;
public unsafe GetLibraryVersionXDelegate GetLibraryVersion;
public unsafe GetLibraryDateXDelegate GetLibraryDate;
public unsafe InitCallbackTableXDelegate InitCallbackTable;
.....
}
当我调用应该初始化 WrapperContext 结构的 LoadContext native 方法时,运行时抛出以下异常:
Attempting to JIT compile method '(wrapper managed-to-native) XXXXX.XXXXXX.XXXXX.GetLibraryVersionXDelegate:wrapper_aot_native (object)' while running with --aot-only. See http://docs.xamarin.com/ios/about/limitations for more information.
为了解决这个问题,我创建了另一个结构,其中包含完全相同的字段列表,但将委托(delegate)类型替换为 IntPtr:
[StructLayout (LayoutKind.Sequential)]
public struct WrapperContext2
{
public int ctxVersion;
public IntPtr GetLibraryVersionXDelegate GetLibraryVersion;
public IntPtr GetLibraryDateXDelegate GetLibraryDate;
public IntPtr InitCallbackTableXDelegate InitCallbackTable;
.....
}
目前,对我的 native LoadContext 的调用工作正常并且我为每个函数获取了一个指针,但是当尝试使用 Marshal.GetFunctionPointerForDelegate() 将函数指针转换为它们的委托(delegate)表示时,完全相同的异常被抛出运行时间:
Attempting to JIT compile method '(wrapper managed-to-native) XXXXX.XXXXXX.XXXXX.GetLibraryVersionXDelegate:wrapper_aot_native (object)' while running with --aot-only. See http://docs.xamarin.com/ios/about/limitations for more information.
感谢您的帮助。
最佳答案
我最终通过在每个委托(delegate)定义上添加 UnmanagedFunctionPointer 属性解决了这个问题:
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public unsafe delegate IntPtr GetLibraryVersionXDelegate();
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public unsafe delegate IntPtr GetLibraryDateXDelegate();
希望这对其他人有帮助。
关于c# - 消息 AOT 问题 Attempting to JIT compile method with Pinvoke on Xamarin iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37726254/