将 RCW 强制转换为 InterfaceIsIInspectable 接口会引发 PlatformNotSupportedException

将运行时可调用包装器 (RCW) 强制转换为标记为 InterfaceIsIInspectable 的接口现在会引发 PlatformNotSupportedException。 此更改是对 从 .NET 中删除 WinRT 支持的后续作。

已引入的版本

.NET 5

更改描述

在以前的 .NET 版本中,将 RCW 强制转换为标记为 InterfaceIsIInspectable 的接口,可以按预期工作。 从 .NET 5 开始,将 RCW 强制转换为标记为 InterfaceIsIInspectable 的接口将在转换时引发 PlatformNotSupportedException

更改原因

InterfaceIsIInspectable的支持已被移除。 由于运行时中的基础支持已经不存在,抛出PlatformNotSupportedException可以提供一个平滑的故障处理路径。 引发异常也使更容易发现此功能已不再受支持。

  • 如果可以在 Windows 运行时元数据(WinMD)文件中定义接口,请改用 C#/WinRT 工具。

  • 否则,请将接口标记为 InterfaceIsIUnknown 代替 InterfaceIsIInspectable,并将三个虚拟条目添加到方法的接口 InterfaceIsIInspectable 的开头。 以下代码片段演示了一个示例。

    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IMine
    {
        // Do not call these three methods.
        // They're exclusively to fill in the slots in the vtable.
        void GetIIdsSlot();
        void GetRuntimeClassNameSlot();
        void GetTrustLevelSlot();
    
        // The original members of the IMine interface go here.
        ...
    }
    

受影响的 API