microsoft / microsoft/CsWinRT

Invalid cast from 'WinRT.IInspectable' to 'System.Collections.Generic.IEnumerable`1[Windows.Storage.IStorageFile]

Open
#747 12 comments 0 reactions 1 assignee View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C#
Stars
665
Forks
134
Avg merge
1d 3h
Merged PRs (30d)
32

Description

This issue is a duplication of the https://github.com/microsoft/microsoft-ui-xaml/issues/4111 that @stevenwdv created and it seems a C#/WinRT projection issue.

Casting a collection of StorageFiles to a collection of IStorageFiles and using it fails at runtime (this may also be the case with other interfaces, not sure. This only happens when using Desktop WinUI 3.

Steps to reproduce the bug

  1. Create a new C# "Blank App, Packaged (WinUI in Desktop)"
  2. Add the following code to e.g. OnLauched in App.xaml.cs:
Task.Run(async () =>
{
    try
    {
        var folder = ApplicationData.Current.LocalFolder;
        var files  = await folder.GetFilesAsync();
        _ = ((IReadOnlyList<IStorageFile>) files).ToList();
    }
    catch (InvalidCastException ex)
    {
        Debugger.Break();
    }
});
  1. Run the package project (enabling break on CLR exceptions recommended)
  2. Observe the InvalidCastException in DynamicInterfaceCastableHelpers.IsInterfaceImplemented

Expected behavior
Should work without exception.

Version Info
[Microsoft.WinUI 3.0.0-preview4.210210.4]

Windows app type:

UWP Win32
No Yes
Windows 10 version Saw the problem?
Insider Build (20H2: 19042.746) Yes
May 2020 Update (19041) Yes
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.