-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5836: Support new Reverse with array overload introduced by .NET 10 #1848
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -144,6 +144,7 @@ internal static class EnumerableMethod | |
| private static readonly MethodInfo __range; | ||
| private static readonly MethodInfo __repeat; | ||
| private static readonly MethodInfo __reverse; | ||
| private static readonly MethodInfo __reverseWithArray; // will be null on target frameworks that don't have this method | ||
| private static readonly MethodInfo __select; | ||
| private static readonly MethodInfo __selectMany; | ||
| private static readonly MethodInfo __selectManyWithCollectionSelectorAndResultSelector; | ||
|
|
@@ -195,6 +196,12 @@ internal static class EnumerableMethod | |
| // static constructor | ||
| static EnumerableMethod() | ||
| { | ||
| #if NET10_OR_GREATER | ||
| __reverseWithArray = ReflectionInfo.Method(array source) => source.Reverse()); | ||
| #else | ||
| __reverseWithArray = GetReverseWithArrayMethodInfo(); // support users running net10 even though we don't target net10 yet | ||
| #endif | ||
|
|
||
| __aggregateWithFunc = ReflectionInfo.Method((IEnumerable<object> source, Func<object, object, object> func) => source.Aggregate(func)); | ||
| __aggregateWithSeedAndFunc = ReflectionInfo.Method((IEnumerable<object> source, object seed, Func<object, object, object> func) => source.Aggregate(seed, func)); | ||
| __aggregateWithSeedFuncAndResultSelector = ReflectionInfo.Method((IEnumerable<object> source, object seed, Func<object, object, object> func, Func<object, object> resultSelector) => source.Aggregate(seed, func, resultSelector)); | ||
|
|
@@ -481,6 +488,7 @@ static EnumerableMethod() | |
| public static MethodInfo Range => __range; | ||
| public static MethodInfo Repeat => __repeat; | ||
| public static MethodInfo Reverse => __reverse; | ||
| public static MethodInfo ReverseWithArray => __reverseWithArray; | ||
| public static MethodInfo Select => __select; | ||
| public static MethodInfo SelectMany => __selectMany; | ||
| public static MethodInfo SelectManyWithCollectionSelectorAndResultSelector => __selectManyWithCollectionSelectorAndResultSelector; | ||
|
|
@@ -613,5 +621,28 @@ public static MethodInfo MakeWhere(Type tsource) | |
| { | ||
| return __where.MakeGenericMethod(tsource); | ||
| } | ||
|
|
||
| #if !NET10_OR_GREATER | ||
|
||
| private static MethodInfo GetReverseWithArrayMethodInfo() | ||
| { | ||
| // returns null on target frameworks that don't have this method | ||
| return | ||
| typeof(Enumerable) | ||
| .GetMethods() | ||
| .SingleOrDefault(m => | ||
| m.IsPublic && | ||
| m.IsStatic && | ||
| m.Name == "Reverse" && | ||
| m.IsGenericMethodDefinition && | ||
| m.GetGenericArguments() is var genericArguments && | ||
| genericArguments.Length == 1 && | ||
| genericArguments[0] is var tsource && | ||
| m.ReturnType == typeof(IEnumerable<>).MakeGenericType(tsource) && | ||
| m.GetParameters() is var parameters && | ||
| parameters.Length == 1 && | ||
| parameters[0] is var sourceParameter && | ||
| sourceParameter.ParameterType == tsource.MakeArrayType()); | ||
| } | ||
| #endif | ||
| } | ||
| } | ||
This file contains hidden or 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
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.
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.
This #if def does not make any sense to me. We do not have any target defining it, so the code is never going to be executed.
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.
It makes sense if you consider that very soon we will be adding net10 as a target.
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.
Probably, but for now it's a dead-code. Why do we need to add this code to repo NOW?
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've came across this question in CSHARP-5665 wanting to use
CommonPrefixLength. I decided not to have a dead and untested code, and will note it as a future improvement fornet10target. I suggest doing the same here.Also we don't have an official date to support net10 yet.