dotnet 6 修复找不到 EnumeratorToEnumVariantMarshaler 问题

我将在一个 .NET Framework 项目升级到 dotnet 6 时发现构建不通过,因为原先的代码使用到了 EnumeratorToEnumVariantMarshaler 类型,在 dotnet 6 里面找不到。本文将告诉大家如何修复此问题

在 .NET Framework 定义的 EnumeratorToEnumVariantMarshaler 类型是在 dotnet core 里缺失的

代替方法是使用 MarshalType 属性写字符串,如以下代码

        [MethodImpl(MethodImplOptions.InternalCall)]
        [DispId(1)]
        [TypeLibFunc(1)]
        [return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalType = "System.Runtime.InteropServices.CustomMarshalers.EnumeratorToEnumVariantMarshaler")]
        IEnumerator GetEnumerator();

参考文档

CustomMarshalers - EnumeratorToEnumVariantMarshaler · Issue #47243 · dotnet/runtime

posted @ 2023-09-05 08:44  lindexi  阅读(14)  评论(0编辑  收藏  举报