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

Bug: "Specified cast is not valid" when calling OnCollectionChanged of ObservableCollection when AOT is enabled #1876

Open
karmeye opened this issue Nov 23, 2024 · 3 comments

Comments

@karmeye
Copy link

karmeye commented Nov 23, 2024

Description

InvalidCastException "Specified cast is not valid." when <PublishAot>true</PublishAot> and calling AddRange, which in turn calls

OnCollectionChanged(
   new NotifyCollectionChangedEventArgs(
        action: NotifyCollectionChangedAction.Add,
	changedItems: collection as System.Collections.IList ?? collection.ToList(),
	startingIndex: Count - count));

Stack trace (also see screenshot below)

at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
at WinRT.ComWrappersSupport.CreateCCWForObjectForABI(Object obj, Guid iid)
at WinRT.ComWrappersSupport.CreateCCWForObjectForMarshaling(Object obj, Guid iid)
at WinRT.MarshalInspectable`1.CreateMarshaler2(T o, Guid iid, Boolean unwrapObject)
at WinRT.MarshalInterface`1.CreateMarshaler2(T value, Guid iid)
at ABI.Microsoft.UI.Xaml.Interop.WinRTNotifyCollectionChangedEventArgsRuntimeClassFactory.CreateInstanceWithAllParameters(NotifyCollectionChangedAction action, IList newItems, IList oldItems, Int32 newIndex, Int32 oldIndex)
at ABI.System.Collections.Specialized.NotifyCollectionChangedEventArgs.CreateMarshaler2(NotifyCollectionChangedEventArgs value)
at ABI.System.Collections.Specialized.NotifyCollectionChangedEventHandler.NativeDelegateWrapper.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
at Abc.Core.ObservableCollectionEx`1.AddRange(IEnumerable`1 collection)

When AOT is disabled there is no exception thrown (even if publishing trimmed).

Steps To Reproduce

  1. Create a WinUI project
  2. Add <PublishAot>true</PublishAot>
  3. Add ObservableCollectionEx from here
  4. Call the AddRange method

Expected Behavior

No exception thrown.

Version Info

  • WindowsAppSdk 1.6.241114003
  • .NET 9

Additional Context

Image

@manodasanW
Copy link
Member

Can you also specify what is the runtime type of the parameter being passed to AddRange?

@karmeye
Copy link
Author

karmeye commented Nov 23, 2024

@manodasanW It is a custom class:

public class PersonItem : IEquatable<PersonItem?>
{
	public PersonItem(...)
	{
	}

	public PersonItem(...)
	{
	}

	public PersonItem(...)
	{
	}

	public PersonItem(...)
	{
        ...
	}

	#region Properties

    public string DisplayName { get; private set; }

    ...

	#endregion

	#region Overrides

	public override string ToString() => DisplayName;

	public override bool Equals(object? obj) => Equals(obj as PersonItem);

	public override int GetHashCode() => HashCode.Combine(Uuid);

	#endregion

	#region Interfaces

	#region IEquatable<T>

	public bool Equals(PersonItem? other)
	{
		return other is not null &&
			   EqualityComparer<Uuid>.Default.Equals(Uuid, other.Uuid);
	}

	#endregion

	#endregion

	#region Operators

	public static bool operator ==(PersonItem? left, PersonItem? right)
	{
		return EqualityComparer<PersonItem>.Default.Equals(left, right);
	}

	public static bool operator !=(PersonItem? left, PersonItem? right)
	{
		return !(left == right);
	}

	#endregion
}

@manodasanW
Copy link
Member

Thanks, is it a list of that you are constructing (i.e. new List<Person>()) or an array or collection expression? It might be worth trying out the latest CsWinRT 2.2.0 release as we do have improved error messages which should say the type name when InvalidCastException happens. This release has not shipped as the default yet in the .NET SDK, so you would need to explicitly specify it to try it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants