Casting RCW to an InterfaceIsIInspectable interface throws PlatformNotSupportedException

Casting a runtime callable wrapper (RCW) to an interface marked as InterfaceIsIInspectable now throws a PlatformNotSupportedException. This change is a follow up to the removal of WinRT support from .NET.

Version introduced

5.0 RC2

Change description

In .NET versions prior to .NET 5 preview 6, casting an RCW to an interface marked as InterfaceIsIInspectable works as expected. In .NET 5 previews 6-8 and RC1, you can successfully cast an RCW to an InterfaceIsIInspectable interface. However, you might get access violations when you execute methods on the interface, because the underlying support in the runtime was removed in .NET 5 preview 6.

In .NET 5 RC2 and later versions, casting an RCW to an interface marked as InterfaceIsIInspectable throws a PlatformNotSupportedException at cast time.

Reason for change

The support for InterfaceIsIInspectable was removed in a previous .NET 5 preview. However, casting to an InterfaceIsIInspectable interface was accidentally overlooked. Since the underlying support in the runtime no longer exists, throwing a PlatformNotSupportedException enables a graceful failure path. Throwing an exception also makes it more discoverable that this feature is no longer supported.

  • If you can define the interface in a Windows runtime metadata (WinMD) file, use the C#/WinRT tool instead.

  • Otherwise, mark the interface as InterfaceIsIUnknown instead of InterfaceIsIInspectable, and add three dummy entries to the start of the interface for the InterfaceIsIInspectable methods. The following code snippet shows an example.

    [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.
        ...
    }
    

Affected APIs