将 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