CsWinRT
CsWinRT copied to clipboard
Bug: AOT doesn't work with interfaces
Description
My code is like below
public class Parent { }
public sealed class Child : Parent { }
public static class Example
{
public static Child GetChild() { return new Child(); }
}
But it doesn't compile because "error CsWinRT1005: Exporting unsealed types is not supported in CsWinRT, please mark type Parent as sealed" I use interfaces as workaround, and change my code to
public interface IParent {}
public interface IChild : IParent {}
internal partial class Parent : IParent {}
internal partial class Child : Parent, IChild {}
public sealed class Example
{
public static IChild GetChild() { return new Child(); }
}
And it works well.
Next, I enable AOT following https://github.com/MichalStrehovsky/CppPublishAotReference When I run the code, I see below error: Exception thrown at 0x00007FFC28D2837A (KernelBase.dll) in CppConsoleApp.exe: WinRT originate error - 0x80004002 : 'Failed to create a CCW for object of type 'AuthoringDemo.Parent' for interface with IID 'F4D7DFF0-F052-5B09-8FB3-7B563C3D0E1C': the specified cast is not valid.'. Exception thrown at 0x00007FFC28D2837A (KernelBase.dll) in CppConsoleApp.exe: WinRT originate error - 0x80004002 : 'Failed to create a CCW for object of type 'AuthoringDemo.Child' for interface with IID '58D127C1-2630-5E63-8877-42BC48D395E8': the specified cast is not valid.'.
Steps To Reproduce
- clone https://github.com/lyf6lyf/CsWinRT/commit/e05e5897303e334e1a310bbada886b6a806eddca
- build and run \src\Samples\AuthoringDemo\CppConsoleApp\main.cpp
Expected Behavior
The code can run successfully.
Version Info
CsWinRT version: 2.2.0
DotNet version: net8.0-windows10.0.22621.0, net9.0-windows10.0.22621.0
Additional Context
No response