-
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
Handle more Attribute Types when Filtering Assemblies #9373
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Code formatting, and log the unknown/unsupported .Kind value in the `catch` block.
Draft commit message: [Xamarin.Android.Build.Tasks] Support HandleKind.TypeSpecification (#9373)
Fixes: https://github.com/dotnet/android/issues/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 |
jonpryor
approved these changes
Oct 8, 2024
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #9369
When System.Reflection.Metadata comes across an Attribute which has a generic, it does NOT return a
TypeReferenceHandle
. It returns aTypeSpecificationHandle
.Tying to cast the type results in the following error
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.