Skip to content

Bug: Got error 'Specified cast is not valid.' on IList with AOT #1998

Open
@DinahK-2SO

Description

@DinahK-2SO

Description

This issue was discovered by developers developing Windows Apps with AOT enabled:
microsoft/WindowsAppSDK#4743 (comment)

The error 'Specified cast is not valid.' was thrown when developer build App with below code:

picker.FileTypeChoices.Add("Test file", [ ".txt" ]);

With new fix in CsWinRT 2.2, the error can be avoided by specifying a List type, for instance, changing above code into:

picker.FileTypeChoices.Add("Test file", new List<string> { ".txt" });

However, the original code still throws error, the difference is that the new error contains more details:

System.InvalidCastException: 'Failed to create a CCW for object of type 'System.Collections.Generic.List'1[System.String]' for interface with IID '98B9ACC1-4B56-532E-AC73-03D5291CCA90': the specified cast is not valid.'

Image

Reporting to the CsWinRT team for further investigation.

Steps To Reproduce

  1. in Visual Studio, create a blank Windows App
    Image
  2. in the *.csproj file, add
    <WindowsSdkPackageVersion>10.0.26100.45</WindowsSdkPackageVersion>
    <PublishAot>true</PublishAot>
  3. update the CsWinRT package to version 2.2.0
    Image
  4. In MainWindows.xaml.cs, override method myButton_Click
        private void myButton_Click(object sender, RoutedEventArgs e)
        {
            var picker = new Windows.Storage.Pickers.FileSavePicker();
            WinRT.Interop.InitializeWithWindow.Initialize(picker, WinRT.Interop.WindowNative.GetWindowHandle(this));
            picker.FileTypeChoices.Add("Test file", [ ".test" ]);
            picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
            myButton.Content = "Clicked";
        }
  5. Debug the app, click button, see the error
    Image
  6. This code can run as a workaround:
    picker.FileTypeChoices.Add("Test file", new List<string> { ".txt" });
  7. for your reference, the FileTypeChoices is IDictionary, whose values are IList
    Image

Expected Behavior

The code should run without error

Version Info

CsWinRT 2.2.0

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions