-
Notifications
You must be signed in to change notification settings - Fork 536
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
MetadataExtensions.GetCustomAttributeFullName crashes on generic AssemblyAttribute #9369
Comments
@inf9144 I tried it in one of our MSBuild integration tests, but this passes: main...jonathanpeppers:xamarin-android:dev/peppers/genericattribute Do I need to set up something differently? Do you have a sample you can share? |
Fixes #9369 When System.Reflection.Metadata comes across an Attribute which has a generic, it does NOT return a `TypeReferenceHandle`. It returns a `TypeSpecificationHandle`. Tying to cast the type results in the following error ``` System.InvalidCastException: Die angegebene Umwandlung ist ungültig. error XAFLT7007: bei System.Reflection.Throw.InvalidCast() error XAFLT7007: bei System.Reflection.Metadata.TypeReferenceHandle.op_Explicit(EntityHandle handle) error XAFLT7007: bei Xamarin.Android.Tasks.MetadataExtensions.GetCustomAttributeFullName(MetadataReader reader, CustomAttribute attribute) error XAFLT7007: bei Xamarin.Android.Tasks.FilterAssemblies.IsAndroidAssembly(AssemblyDefinition assembly, MetadataReader reader) error XAFLT7007: bei Xamarin.Android.Tasks.FilterAssemblies.ProcessAssembly(ITaskItem assemblyItem, List`1 output) error XAFLT7007: bei Xamarin.Android.Tasks.FilterAssemblies.RunTask() error XAFLT7007: bei Microsoft.Android.Build.Tasks.AndroidTask.Execute() ``` So we need to handle this case, and put in place a backup to handle an `InvalidCastException` so that we never hit this issue again.
I reproduced it a second time - crashes on VS 17.11.4 with standard MauiApp template and two class Libraries ClassLibrary2 content:
ClassLibrary1 references ClassLibrary2:
ClassLibrary1 content:
MauiApp references ClassLibrary1:
I hope that helps :-) |
Fixes #9369 When System.Reflection.Metadata comes across an Attribute which has a generic, it does NOT return a `TypeReferenceHandle`. It returns a `TypeSpecificationHandle`. Tying to cast the type results in the following error ``` System.InvalidCastException: Die angegebene Umwandlung ist ungültig. error XAFLT7007: bei System.Reflection.Throw.InvalidCast() error XAFLT7007: bei System.Reflection.Metadata.TypeReferenceHandle.op_Explicit(EntityHandle handle) error XAFLT7007: bei Xamarin.Android.Tasks.MetadataExtensions.GetCustomAttributeFullName(MetadataReader reader, CustomAttribute attribute) error XAFLT7007: bei Xamarin.Android.Tasks.FilterAssemblies.IsAndroidAssembly(AssemblyDefinition assembly, MetadataReader reader) error XAFLT7007: bei Xamarin.Android.Tasks.FilterAssemblies.ProcessAssembly(ITaskItem assemblyItem, List`1 output) error XAFLT7007: bei Xamarin.Android.Tasks.FilterAssemblies.RunTask() error XAFLT7007: bei Microsoft.Android.Build.Tasks.AndroidTask.Execute() ``` So we need to handle this case, and put in place a backup to handle an `InvalidCastException` so that we never hit this issue again.
…9373) Fixes: #9369 [C# 11 added support for generic custom attributes][0]: [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)] public class GenericTestAttribute<T> : Attribute { } which means generic types can now be used as assembly-level attributes: [assembly: GenericTestAttribute<string>] Unfortunately, attempting to do this would result in an `InvalidCastException` from the `<FilterAssemblies/>` task: System.InvalidCastException: Die angegebene Umwandlung ist ungültig. error XAFLT7007: bei System.Reflection.Throw.InvalidCast() error XAFLT7007: bei System.Reflection.Metadata.TypeReferenceHandle.op_Explicit(EntityHandle handle) error XAFLT7007: bei Xamarin.Android.Tasks.MetadataExtensions.GetCustomAttributeFullName(MetadataReader reader, CustomAttribute attribute) error XAFLT7007: bei Xamarin.Android.Tasks.FilterAssemblies.IsAndroidAssembly(AssemblyDefinition assembly, MetadataReader reader) error XAFLT7007: bei Xamarin.Android.Tasks.FilterAssemblies.ProcessAssembly(ITaskItem assemblyItem, List`1 output) error XAFLT7007: bei Xamarin.Android.Tasks.FilterAssemblies.RunTask() error XAFLT7007: bei Microsoft.Android.Build.Tasks.AndroidTask.Execute() This happens because when System.Reflection.Metadata comes across an Attribute which has a generic type parameter, it does *not* return a [`TypeReferenceHandle`][1], but instead a [`TypeSpecificationHandle`][2]. Update `MetadataExtensions.GetCustomAttributeFullName()` to support [`HandleKind.TypeSpecification`][3] [0]: https://learn.microsoft.com/dotnet/csharp/whats-new/csharp-11#generic-attributes [1]: https://learn.microsoft.com/dotnet/api/system.reflection.metadata.typereferencehandle?view=net-8.0 [2]: https://learn.microsoft.com/dotnet/api/system.reflection.metadata.typespecificationhandle?view=net-8.0 [3]: https://learn.microsoft.com/dotnet/api/system.reflection.metadata.handlekind?view=net-8.0
Android framework version
net8.0-android
Affected platform version
VS 2022 17.11.4
Description
If you reference assemblies in your project build that are attributed by generic attributes like:
the build crashes with:
Steps to Reproduce
Did you find any workaround?
Yes - i am the author of the generic attribute and for now fixed it to:
Relevant log output
The text was updated successfully, but these errors were encountered: