Bug: Trying to enumerate the result of DisplayArea.FindAll() throws an InvalidCastException
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 665
- Forks
- 134
- Avg merge
- 1d 3h
- Merged PRs (30d)
- 32
Description
Description
With AOT on, call Microsoft.UI.Windowing.DisplayArea.FindAll().ToList() or anything else that tries to call GetEnumerator on the result of DisplayArea.FindAll() and it fails with an InvalidCastException. The stack trace is
[System.InvalidCastException user-unhandled]
> WinRT.Runtime.dll!WinRT.IObjectReference.As<WinRT.Interop.IUnknownVftbl>(System.Guid iid) Line 139 C#
WinRT.Runtime.dll!System.Collections.Generic.IReadOnlyListImpl<Microsoft.UI.Windowing.DisplayArea>.Make_IEnumerableObjRef() Line 47 C#
WinRT.Runtime.dll!System.Collections.Generic.IReadOnlyListImpl<Microsoft.UI.Windowing.DisplayArea>.iEnumerableObjRef.get() Line 25 C#
WinRT.Runtime.dll!System.Collections.Generic.IReadOnlyListImpl<Microsoft.UI.Windowing.DisplayArea>.GetEnumerator() Line 75 C#
Note that I wouldn't say this was a duplicate of https://github.com/microsoft/CsWinRT/issues/747 as previously suggested in this issue https://github.com/microsoft/microsoft-ui-xaml/issues/6454 since no covariance is involved here. Also, this is not the System.ArgumentOutOfRangeException mentioned in that issue. The workaround of using an integer indexer as in https://github.com/microsoft/microsoft-ui-xaml/issues/6454#issuecomment-2188377618 works.
Steps To Reproduce
See description
Expected Behavior
No exception
Version Info
CsWinRT 2.2, WindowsAppSdk.WinUI 1.8.260204000 (+ transitive dependencies), Windows 25H2.
Additional Context
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the AOT failure with Microsoft.UI.Windowing.DisplayArea.FindAll().ToList() and inspect the WinRT.Runtime stack entries for IReadOnlyListImpl<Microsoft.UI.Windowing.DisplayArea>, Make_IEnumerableObjRef(), and GetEnumerator(). Done means enumeration succeeds without InvalidCastException while the existing integer-indexer workaround remains unnecessary.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100