CsWinRT
CsWinRT copied to clipboard
AoT: Failing to cast object to byte[]
Describe the bug
To Reproduce
- Create a new WinUI 3 app, and enable it for AoT
- 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);
}
- Run the app and click the button.
- 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"])