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

.NET 5

Change description

In previous .NET versions, casting an RCW to an interface marked as InterfaceIsIInspectable worked as expected. Starting in .NET 5, casting an RCW to an interface marked as InterfaceIsIInspectable throws a PlatformNotSupportedException at cast time.

Reason for change

The support for InterfaceIsIInspectable was removed. 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