CsWinRT icon indicating copy to clipboard operation
CsWinRT copied to clipboard

AoT: Failing to cast object to byte[]

Open dotMorten opened this issue 1 year ago • 6 comments

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 avatar Sep 20 '24 22:09 dotMorten