CsWinRT icon indicating copy to clipboard operation
CsWinRT copied to clipboard

IObjectReference.As<T> completely breaks down on NativeAOT (due to MakeGenericMethod)

Open Sergio0694 opened this issue 2 years ago • 0 comments

Describe the bug

Note: related to #1248. This is just meant for tracking, as I'm aware CsWinRT doesn't support NativeAOT just yet.

I've put together a minimal Win2D Win32 sample, and tried to get it to run on NativeAOT. The sample is here: https://github.com/Sergio0694/Win2DSample. It does work, but I had to include two .rd.xml directives to work around two MakeGenericMethod calls:

<Type Name="WinRT.IObjectReference">
  <Method Name="As" Dynamic="Required All">
    <GenericArgument Name="ABI.Microsoft.Graphics.Canvas.ICanvasFactoryNative+Interface+Vftbl, Win2DRenderer" />
  </Method>
  <Method Name="As" Dynamic="Required All">
    <GenericArgument Name="ABI.System.IDisposable+Vftbl, WinRT.Runtime" />
  </Method>
</Type>

In general, it seems IObjectReference.As<T> is fundamentally non-AOT-friendly. Specifically, this line causes issues:

https://github.com/microsoft/CsWinRT/blob/e644f9cca17612adea4d726bef02069eb422f9aa/src/WinRT.Runtime/IWinRTObject.net5.cs#L144

EDIT: actually I'm not 100% sure it's exactly that line, as that seems to be in a fallback path which shouldn't be taken? 🤔

Sergio0694 avatar Apr 16 '23 19:04 Sergio0694