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

[mono] Allow overriding GetCustomAttributesData routines #55726

Merged
1 commit merged into from
Jul 16, 2021
Merged

[mono] Allow overriding GetCustomAttributesData routines #55726

1 commit merged into from
Jul 16, 2021

Conversation

uweigand
Copy link
Contributor

  • Have CustomAttributeData.GetCustomAttributes call target.GetCustomAttributesData
    to allow derived classes to override the latter (like in CoreCLR)

System.Text.Json.SourceGeneration.Tests are currently failing when using a Mono-based dotnet runtime. This is because the source-generation logic overrides GetCustomAttributesData in various derived classes, and expects this to be called from the generic CustomAttributeData.GetCustomAttributes routine. This works in CoreCLR, because there CustomAttributeData.GetCustomAttributes calls target.GetCustomAttributesData which calls back to CustomAttributeData.GetCustomAttributesInternal, which does the actual work. However, the current Mono code has instead target.GetCustomAttributesData calling to CustomAttributeData.GetCustomAttributes, which does the actual work.

Fix this by introducing GetCustomAttributesInternal routines and re-arranging the call sequence to match CoreCLR.

@lambdageek
Copy link
Member

@uweigand maybe we can pull the common bit of CustomAttributeData into a new file under src/libraries and share it between the two runtimes?

public class CustomAttributeData
{
#region Public Static Members
public static IList<CustomAttributeData> GetCustomAttributes(MemberInfo target)
{
if (target is null)
throw new ArgumentNullException(nameof(target));
return target.GetCustomAttributesData();
}
public static IList<CustomAttributeData> GetCustomAttributes(Module target)
{
if (target is null)
throw new ArgumentNullException(nameof(target));
return target.GetCustomAttributesData();
}
public static IList<CustomAttributeData> GetCustomAttributes(Assembly target)
{
if (target is null)
throw new ArgumentNullException(nameof(target));
return target.GetCustomAttributesData();
}
public static IList<CustomAttributeData> GetCustomAttributes(ParameterInfo target)
{
if (target is null)
throw new ArgumentNullException(nameof(target));
return target.GetCustomAttributesData();
}
#endregion

@uweigand
Copy link
Contributor Author

@uweigand maybe we can pull the common bit of CustomAttributeData into a new file under src/libraries and share it between the two runtimes?

public class CustomAttributeData
{
#region Public Static Members
public static IList<CustomAttributeData> GetCustomAttributes(MemberInfo target)
{
if (target is null)
throw new ArgumentNullException(nameof(target));
return target.GetCustomAttributesData();
}
public static IList<CustomAttributeData> GetCustomAttributes(Module target)
{
if (target is null)
throw new ArgumentNullException(nameof(target));
return target.GetCustomAttributesData();
}
public static IList<CustomAttributeData> GetCustomAttributes(Assembly target)
{
if (target is null)
throw new ArgumentNullException(nameof(target));
return target.GetCustomAttributesData();
}
public static IList<CustomAttributeData> GetCustomAttributes(ParameterInfo target)
{
if (target is null)
throw new ArgumentNullException(nameof(target));
return target.GetCustomAttributesData();
}
#endregion

I guess so ... but is it worthwhile doing this now, just for these trivial bits? I think in the end most of the custom attribute logic should be shareable between Mono and CoreCLR, but that will need more cleanup.

@lambdageek
Copy link
Member

I guess so ... but is it worthwhile doing this now, just for these trivial bits?

Yea that's exactly my question. 😄

I guess one reason to do it on a small relatively trivial PR is that it would be pretty easy to make sense of the diff, compared to something deeper.

But it's up to you. I certainly don't have a problem with the PR as is.

I think in the end most of the custom attribute logic should be shareable between Mono and CoreCLR, but that will need more cleanup.

Yea I think compared to elsewhere in Reflection the differences here are more incidental, so we should be able to converge.

@uweigand
Copy link
Contributor Author

I'd prefer to go with the PR as-is for now. I can look into what can be merged here a bit later (probably best after .NET 6 has branched).

@uweigand
Copy link
Contributor Author

New version that moves common bits to the shared location. This includes moving ToString, GetHashCode, Equals, and AttributeType, which either were already identical or could trivially be made identical. Note that this obsoletes #55728 as well as the new code already allows overriding the accessor routines.

@marek-safar
Copy link
Contributor

marek-safar commented Jul 16, 2021

New version that moves common bits to the shared location.

Thanks! You'll need to update coreclr implementation too to avoid code duplication.

* Merge common bits of Mono and CoreCLR CustomAttributeData implementation

* Have CustomAttributeData.GetCustomAttributes call target.GetCustomAttributesData
  to allow derived classes to override the latter (like in CoreCLR)

* Use Constructor, ConstructorArguments, and NamedArguments in internal
  routines to allow them being overridden (like with CoreCLR).
@ghost
Copy link

ghost commented Jul 16, 2021

Hello @marek-safar!

Because this pull request has the auto-merge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

3 participants