microsoft / microsoft/CsWinRT

Bug: InvalidCastException because failed to create a CCW in AOT for IAsyncOperation<>

Open
#2,378 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

An WinRT API described below:

public static class MyClass
{
    public static IAsyncOperation<IReadOnlyList<MyData>> TestAsync ()
    {
        return AsyncInfo.Run((CancelToken) => new ReadOnlyCollection<MyData>([new MyData()]));
    }
}

Everything OK if AOT not enabled. However, once AOT enabled. The caller will get an InvalidCastException. Failed to create a CCW for ReadOnlyCollection<MyData>.

After adding the code below, it works again in AOT.

[assembly: GeneratedWinRTExposedExternalType(typeof(ReadOnlyCollection<MyData>))]

I want to know will this exception be fixed in CsWinRT 2.3 and adding GeneratedWinRTExposedExternalType for the generic collection is just a temporary workaround? Or it's not a bug and we should always use GeneratedWinRTExposedExternalType to every generic collection variant to make it works in AOT?

What is the difference for my case after #1877 collection expression implemented in CsWinRT 2.3 ?

public static IAsyncOperation<IReadOnlyList<MyData>> TestAsync ()
{
    // After CsWinRT 2.3, this will also works?
    // Will we still need GeneratedWinRTExposedExternalType(typeof(ReadOnlyCollection<MyData>)) ?
    return AsyncInfo.Run((CancelToken) => [new MyData()]);
    // return AsyncInfo.Run((CancelToken) => new ReadOnlyCollection<MyData>([new MyData()]));
}
Steps To Reproduce

See code above

Expected Behavior

Should generate the CCW automatically for anything like

IAsyncOperation<IReadOnlyList<>>
IAsyncOperation<IList<>>
IAsyncOperation<IEnumerable<>>
IAsyncOperation<ICollection<>>
Version Info

Microsoft.Windows.CsWinRT 2.2.0

Additional Context

No response

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.

Research direction

Reproduce the AOT failure with the IAsyncOperation<IReadOnlyList> sample and compare it with the GeneratedWinRTExposedExternalType workaround. Trace the CCW generation path for ReadOnlyCollection, then compare the collection-expression case described in the issue. Done means the supported generic collection variants create CCWs automatically, or the required workaround is clearly documented and covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.