c# - 消息 AOT 问题 Attempting to JIT compile method with Pinvoke on Xamarin iOS

标签 c# c++ ios xamarin pinvoke

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

相关文章:

ios - 来自数据模型的对象中的数组为零

iphone - 在 iOS 中设置 unicode 字符宽度

c# - .NET C# 将 ResourceSet 转换为 JSON

c# - asp.net mvc c# with sql 没有 ORM

c++ - 带有 "noexcept"构造函数的程序被 gcc 接受,被 clang 拒绝

c++ - 如何使用 std::pair 作为键 std::map

c++ - 如何评估交换机中的QChar对象?

c# - 将通知模板从应用程序后端注册到 Azure 通知中心

c# - 如何在 dotnetcore xunit 测试中设置文化

ios - 如何在 ViewController 之间传递委托(delegate)