Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AoT: Failing to cast object to byte[] #1796

Open
dotMorten opened this issue Sep 20, 2024 · 6 comments
Open

AoT: Failing to cast object to byte[] #1796

dotMorten opened this issue Sep 20, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@dotMorten
Copy link
Contributor

dotMorten commented Sep 20, 2024

Describe the bug

To Reproduce

  1. Create a new WinUI 3 app, and enable it for AoT
  2. Replace the MainWindow.xaml.cs code with the following:
        public MainWindow()
        {
            var settings = GetLocalSettings();
            if (!settings.Values.ContainsKey("Test"))
                settings.Values.Add("Test", new byte[] { 1, 2, 3, 4 });
            this.InitializeComponent();
        }

        private void myButton_Click(object sender, RoutedEventArgs e)
        {
            var settings = GetLocalSettings();
            var data = (byte[])settings.Values["Test"](); // BOOM! Unable to cast object of type 'WinRT.IInspectable' to type 'System.Byte[]'.
        }

        private ApplicationDataContainer GetLocalSettings()
        {
            var localSettings = ApplicationData.Current.LocalSettings;
            if (localSettings.Containers.TryGetValue("Settings", out var container))
                return container;
            return localSettings.CreateContainer("Settings", ApplicationDataCreateDisposition.Always);
        }
  1. Run the app and click the button.
  2. Observe the application crash: Unable to cast object of type 'WinRT.IInspectable' to type 'System.Byte[]'.

Expected behavior
App settings gets the byte[].

Version Info
WinAppSDK 1.6.0, CSWinRT 2.1.3

Additional context
Also tried:

  • .As<byte[]>()
  • .As<IEnumerable<byte>>().ToArray()
  • Enumerable.Cast<byte[]>((IEnumerable)settings.Values["Test"])
@dotMorten dotMorten added the bug Something isn't working label Sep 20, 2024
@dongle-the-gadget
Copy link
Contributor

dongle-the-gadget commented Sep 20, 2024

I was under the impression that arrays are illegal to use outside of parameters in the WinRT type system.

https://learn.microsoft.com/en-us/uwp/winrt-cref/winrt-type-system#array-parameters

@dotMorten
Copy link
Contributor Author

dotMorten commented Sep 20, 2024

@dongle-the-gadget are you saying we can't store any binary data (like data encrypted with the DataProtectionProvider) in the application settings? I mean it works fine - it's only in AoT mode it doesn't. .NET Native with UWP works fine also.

@dongle-the-gadget
Copy link
Contributor

dongle-the-gadget commented Sep 21, 2024

It works but seems like undocumented behavior. The supported list of types in docs also doesn’t mention arrays. I suppose though that you can probably cast the object into an IPropertyValue and get the array from there.

@dongle-the-gadget
Copy link
Contributor

By any chance did you disable IReference support?

@dotMorten
Copy link
Contributor Author

By any chance did you disable IReference support?

Not that I know off? The code is in a net8.0-windows library. No explicit reference to WindowsAppSDK.

@manodasanW
Copy link
Member

This works via boxing support / IReferenceArray. It looks like even though we pass a managed array initially, the array we are getting back is a native version of it. When we do, we are failing to construct the type to project it as on AOT due to being a generic with a value type which is guarded against on AOT and thereby fallback to IInspectable. I am looking at doing something similar to what we did for IReference / nullable here to get it working at least for blittable array scenarios.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants