Skip to content

Slightly better support for NativeAOT and trimming #35211

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

Merged
merged 2 commits into from
Aug 10, 2021

Conversation

davidfowl
Copy link
Member

  • Use explicit reflection to make sure primitive TryParse method are preserved for trimming
  • Also use explicit reflection to preserve HttpContext properties when using expression tree compilation.
  • This doesn't work for custom types (those need to be preserved by the application).

Follow up to this conversation

cc @jkotas @MichalStrehovsky @eerhardt

- Use explicit reflection to make sure primitive TryParse method are preserved for trimming
- Also use explicit reflection to preserve HttpContext properties when using expression tree compilation.
- This doesn't work for custom types (those need to be preserved by the application).
@davidfowl davidfowl requested review from halter73 and pranavkm August 10, 2021 04:20
@davidfowl davidfowl added feature-minimal-actions Controller-like actions for endpoint routing area-runtime old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels and removed area-runtime labels Aug 10, 2021
}
else
{
foreach (var method in typeof(Enum).GetMethods(BindingFlags.Public | BindingFlags.Static))
Copy link
Member

Choose a reason for hiding this comment

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

methodInfo = typeof(Enum).GetMethod(nameof(Enum.TryParse), 1, new Type[] { typeof(String), Type.MakeGenericMethodParameter(0).MakeByRefType() });

Might be a bit shorter. Also, trimming will know to keep just the specific method.

typeof(Enum).GetMethods(BindingFlags.Public | BindingFlags.Static) keeps all public static methods on System.Enum.

Copy link
Member Author

Choose a reason for hiding this comment

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

I wasn't sure how to write this. Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

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

I feel like I've just leveled up my reflection powers...

Copy link
Member

Choose a reason for hiding this comment

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

I feel like I've just leveled up my reflection powers...

Glad I helped! But also, I'm a bit scared.

Copy link
Member Author

Choose a reason for hiding this comment

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

🤣

Copy link
Member Author

Choose a reason for hiding this comment

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

@pranavkm come look at this magic.

- Level up generic reflection
- Fix issues with TryParse
Comment on lines +129 to +132
methodInfo = typeof(Enum).GetMethod(
nameof(Enum.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(Type), typeof(string), typeof(object).MakeByRefType() });
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
methodInfo = typeof(Enum).GetMethod(
nameof(Enum.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(Type), typeof(string), typeof(object).MakeByRefType() });
methodInfo = typeof(Enum).GetMethod(
nameof(Enum.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(Type), typeof(string), typeof(object).MakeByRefType() });

Comment on lines +136 to +139
methodInfo = typeof(Enum).GetMethod(
nameof(Enum.TryParse),
genericParameterCount: 1,
new[] { typeof(string), Type.MakeGenericMethodParameter(0).MakeByRefType() });
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
methodInfo = typeof(Enum).GetMethod(
nameof(Enum.TryParse),
genericParameterCount: 1,
new[] { typeof(string), Type.MakeGenericMethodParameter(0).MakeByRefType() });
methodInfo = typeof(Enum).GetMethod(
nameof(Enum.TryParse),
genericParameterCount: 1,
new[] { typeof(string), Type.MakeGenericMethodParameter(0).MakeByRefType() });

Comment on lines +194 to +197
method = typeof(long).GetMethod(
nameof(long.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(long).MakeByRefType() });
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
method = typeof(long).GetMethod(
nameof(long.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(long).MakeByRefType() });
method = typeof(long).GetMethod(
nameof(long.TryParse),
BindingFlags.Public | BindingFlags.Static,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(long).MakeByRefType() });

}
else if (type == typeof(int))
{
method = typeof(int).GetMethod(
Copy link
Contributor

Choose a reason for hiding this comment

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

Formatting for all of these

@davidfowl davidfowl merged commit fa9dd9c into main Aug 10, 2021
@davidfowl davidfowl deleted the davidfowl/native-aot-minimal-api branch August 10, 2021 19:13
@ghost ghost added this to the 6.0-rc1 milestone Aug 10, 2021
@amcasey amcasey added the area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc label Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-minimal-actions Controller-like actions for endpoint routing old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants