Skip to content

Conversation

madelson
Copy link
Contributor

@madelson madelson commented Mar 13, 2022

Restore the reverted PR (https://github.com/dotnet/runtime/pull/65237/files) but this time handle DynamicMethod
Fixes 66496

This change makes DynamicMethod.GetCustomAttributes() compatible with
Attribute.GetCustomAttributes().

Fix dotnet#66496
@ghost ghost added area-System.Reflection community-contribution Indicates that the PR has been added by a community member labels Mar 13, 2022
@ghost
Copy link

ghost commented Mar 13, 2022

Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

Issue Details

Restore the reverted PR (https://github.com/dotnet/runtime/pull/65237/files) but this time handle DynamicMethod

Author: madelson
Assignees: -
Labels:

area-System.Reflection

Milestone: -

@jkotas
Copy link
Member

jkotas commented Mar 13, 2022

The new test is failing on Mono:

[02:46:22] fail: [FAIL] System.Tests.AttributeGetCustomAttributes.GetCustomAttributes_DynamicMethod
[02:46:22] info: Assert.Equal() Failure
[02:46:22] info: Expected: 1
[02:46:22] info: Actual:   0
[02:46:22] info:    at System.Tests.AttributeGetCustomAttributes.GetCustomAttributes_DynamicMethod()
[02:46:22] info:    at System.Reflection.RuntimeMethodInfo.InvokeWorker(Object obj, BindingFlags invokeAttr, Span`1 parameters)
[02:48:28] info: Finished:    System.Runtime.Tests.dll

@madelson
Copy link
Contributor Author

The new test is failing on Mono:

@jkotas I spent some time looking into this and it seems to me that there is a bit of a web to untangle. TL;DR I set the test to skip on Mono.

First off, unlike with coreclr Mono's attempt to support MethodImplAttribute in DynamicMethod does not extend to the compiled method. That means you get a different answer when you call GetCustomAttributes() on the original DynamicMethod instance vs. the Method property of the created Delegate. On CoreCLR these call through to the same code in RTDynamicMethod. To solve for this I think we'd need Mono start using something like RTDynamicMethod.

If we restrict the problem to just looking at calling Attribute.GetCustomAttributes() on the DynamicMethod instance directly, there is a separate issue. Because CustomAttribute.IsUserCattrProvider thinks DynamicMethod should be treated as "native", it won't actually call through to DynamicMethod.GetCustomAttributes(). This is easy enough to change with a tweak to CustomAttribute.IsUserCattrProvider, but then we run into the issue that CustomAttribute.GetCustomAttributesBase() ends up calling the underlying GetCustomAttributes() passing null for the attribute type when the type starts out as typeof(Attribute) or is not provided. There are multiple FIXME comments in the file warning about this. I don't think this would be a huge deal to fix, but the cleanest way I can see to do it (using typeof(object) as the sole means to request unfiltered attributes) would result in a good number of changes to CustomAttribute.cs and Attribute.Mono.cs. Without more context, it's hard for me to say whether those changes might have other unforseen consequences.

In short, fixing either of these felt out of scope for the original problem; from what I can tell these are gaps/oddities in the Mono attribute functionality that are preexisting and not related to generic attributes at all.

Thoughts?

@jkotas
Copy link
Member

jkotas commented Mar 14, 2022

I agree with your assessment.

@jkotas jkotas requested a review from buyaa-n March 14, 2022 03:03
Copy link
Contributor

@deeprobin deeprobin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@buyaa-n buyaa-n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@buyaa-n
Copy link
Contributor

buyaa-n commented Mar 14, 2022

Build failures unrelated and known issue, merging

@buyaa-n buyaa-n merged commit 917a0b1 into dotnet:main Mar 14, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Apr 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Reflection community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generic attributes: Attribute.GetCustomAttributes returns null when passed an unbound generic type
5 participants