-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Conversation
- 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).
src/Shared/TryParseMethodCache.cs
Outdated
} | ||
else | ||
{ | ||
foreach (var method in typeof(Enum).GetMethods(BindingFlags.Public | BindingFlags.Static)) |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤣
There was a problem hiding this comment.
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
methodInfo = typeof(Enum).GetMethod( | ||
nameof(Enum.TryParse), | ||
BindingFlags.Public | BindingFlags.Static, | ||
new[] { typeof(Type), typeof(string), typeof(object).MakeByRefType() }); |
There was a problem hiding this comment.
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), | |
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() }); |
methodInfo = typeof(Enum).GetMethod( | ||
nameof(Enum.TryParse), | ||
genericParameterCount: 1, | ||
new[] { typeof(string), Type.MakeGenericMethodParameter(0).MakeByRefType() }); |
There was a problem hiding this comment.
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), | |
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() }); |
method = typeof(long).GetMethod( | ||
nameof(long.TryParse), | ||
BindingFlags.Public | BindingFlags.Static, | ||
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider), typeof(long).MakeByRefType() }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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( |
There was a problem hiding this comment.
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
Follow up to this conversation
cc @jkotas @MichalStrehovsky @eerhardt