-
-
Notifications
You must be signed in to change notification settings - Fork 233
Update PredefinedMethodsHelper #911
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
Open
StefH
wants to merge
14
commits into
master
Choose a base branch
from
stef-PredefinedMethodsHelper
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c25c3a3
Update PredefinedMethodsHelper
StefH 3101ebd
...
StefH b1468e1
tasks.json
StefH bd46322
x
StefH 3f92c1c
Updates
StefH 52acb73
Merge branch 'master' into stef-PredefinedMethodsHelper
StefH 8214f0a
Merge branch 'master' into stef-PredefinedMethodsHelper
StefH 7688690
Merge branch 'master' into stef-PredefinedMethodsHelper
StefH ed51756
Merge branch 'master' into stef-PredefinedMethodsHelper
StefH e5a101c
Merge branch 'master' into stef-PredefinedMethodsHelper
StefH 833008a
Merge branch 'master' into stef-PredefinedMethodsHelper
StefH 51df80e
Merge branch 'master' into stef-PredefinedMethodsHelper
StefH 1c16922
Merge branch 'master' into stef-PredefinedMethodsHelper
StefH e289618
.
StefH 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 |
---|---|---|
@@ -1,17 +1,8 @@ | ||
{ | ||
// See http://go.microsoft.com/fwlink/?LinkId=733558 | ||
// for the documentation about the tasks.json format | ||
"version": "0.1.0", | ||
"version": "2.0.0", | ||
"command": "dotnet", | ||
"isShellCommand": true, | ||
"args": [], | ||
"tasks": [ | ||
{ | ||
"taskName": "build", | ||
"args": [ ], | ||
"isBuildCommand": true, | ||
"showOutput": "silent", | ||
"problemMatcher": "$msCompile" | ||
} | ||
] | ||
"tasks": [] | ||
} |
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
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
94 changes: 77 additions & 17 deletions
94
src/System.Linq.Dynamic.Core/Parser/PredefinedMethodsHelper.cs
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 |
---|---|---|
@@ -1,30 +1,90 @@ | ||
using System.Collections.Generic; | ||
using System.Linq.Dynamic.Core.Validation; | ||
using System.Reflection; | ||
|
||
namespace System.Linq.Dynamic.Core.Parser; | ||
|
||
internal static class PredefinedMethodsHelper | ||
internal class PredefinedMethodsHelper | ||
{ | ||
internal static readonly MethodInfo ObjectToString = typeof(object).GetMethod(nameof(ToString), BindingFlags.Instance | BindingFlags.Public, null, Type.EmptyTypes, null)!; | ||
internal static readonly MethodInfo ObjectInstanceEquals = typeof(object).GetMethod(nameof(Equals), BindingFlags.Instance | BindingFlags.Public, null, [typeof(object)], null)!; | ||
internal static readonly MethodInfo ObjectStaticEquals = typeof(object).GetMethod(nameof(Equals), BindingFlags.Static | BindingFlags.Public, null, [typeof(object), typeof(object)], null)!; | ||
internal static readonly MethodInfo ObjectStaticReferenceEquals = typeof(object).GetMethod(nameof(ReferenceEquals), BindingFlags.Static | BindingFlags.Public, null, [typeof(object), typeof(object)], null)!; | ||
private const BindingFlags PublicInstance = BindingFlags.Public | BindingFlags.Instance; | ||
private const BindingFlags PublicStatic = BindingFlags.Public | BindingFlags.Static; | ||
|
||
internal static readonly MethodInfo ObjectInstanceToString = typeof(object).GetMethod(nameof(ToString), PublicInstance, null, Type.EmptyTypes, null)!; | ||
internal static readonly MethodInfo ObjectInstanceEquals = typeof(object).GetMethod(nameof(Equals), PublicInstance, null, [typeof(object)], null)!; | ||
internal static readonly MethodInfo ObjectStaticEquals = typeof(object).GetMethod(nameof(Equals), PublicStatic, null, [typeof(object), typeof(object)], null)!; | ||
internal static readonly MethodInfo ObjectStaticReferenceEquals = typeof(object).GetMethod(nameof(ReferenceEquals), PublicStatic, null, [typeof(object), typeof(object)], null)!; | ||
|
||
private static readonly HashSet<MemberInfo> ObjectToStringAndObjectEquals = | ||
[ | ||
ObjectToString, | ||
ObjectInstanceEquals, | ||
ObjectStaticEquals, | ||
ObjectStaticReferenceEquals | ||
]; | ||
private readonly Dictionary<Type, HashSet<MemberInfo>> _supported = new() | ||
{ | ||
{ typeof(bool), [] }, | ||
{ typeof(byte), [] }, | ||
{ typeof(char), [] }, | ||
{ typeof(DateTime), [] }, | ||
{ typeof(DateTimeOffset), [] }, | ||
{ typeof(decimal), [] }, | ||
{ typeof(double), [] }, | ||
{ typeof(float), [] }, | ||
{ typeof(Guid), [] }, | ||
{ typeof(int), [] }, | ||
{ typeof(long), [] }, | ||
{ typeof(sbyte), [] }, | ||
{ typeof(short), [] }, | ||
{ typeof(string), [] }, | ||
{ typeof(TimeSpan), [] }, | ||
{ typeof(uint), [] }, | ||
{ typeof(ulong), [] }, | ||
{ typeof(Uri), [] }, | ||
{ typeof(ushort), [] }, | ||
#if NET6_0_OR_GREATER | ||
{ typeof(DateOnly), [] }, | ||
{ typeof(TimeOnly), [] } | ||
#endif | ||
}; | ||
|
||
internal PredefinedMethodsHelper(ParsingConfig config) | ||
{ | ||
foreach (var kvp in _supported) | ||
{ | ||
TryAdd(kvp.Key, kvp.Key.GetMethod(nameof(Equals), PublicInstance, null, [kvp.Key], null)); | ||
TryAdd(kvp.Key, kvp.Key.GetMethod(nameof(Equals), PublicInstance, null, [typeof(object)], null)); | ||
|
||
TryAdd(kvp.Key, kvp.Key.GetMethod(nameof(ToString), PublicInstance, null, Type.EmptyTypes, null)); | ||
TryAdd(kvp.Key, kvp.Key.GetMethod(nameof(ToString), PublicInstance, null, [typeof(string)], null)); | ||
TryAdd(kvp.Key, kvp.Key.GetMethod(nameof(ToString), PublicInstance, null, [typeof(IFormatProvider)], null)); | ||
TryAdd(kvp.Key, kvp.Key.GetMethod(nameof(ToString), PublicInstance, null, [typeof(string), typeof(IFormatProvider)], null)); | ||
} | ||
|
||
if (config.AllowEqualsAndToStringMethodsOnObject) | ||
{ | ||
_supported[typeof(object)] = [ObjectInstanceToString, ObjectInstanceEquals, ObjectStaticEquals, ObjectStaticReferenceEquals]; | ||
} | ||
} | ||
|
||
public static bool IsPredefinedMethod(ParsingConfig config, MemberInfo member) | ||
internal bool IsPredefinedMethod(Type type, Type declaringType, MemberInfo member) | ||
{ | ||
Check.NotNull(config); | ||
Check.NotNull(member); | ||
if (_supported.TryGetValue(type, out var supportedMethodsForType) && supportedMethodsForType.Count > 0) | ||
{ | ||
return supportedMethodsForType.Contains(member); | ||
} | ||
|
||
return config.AllowEqualsAndToStringMethodsOnObject && ObjectToStringAndObjectEquals.Contains(member); | ||
if (_supported.TryGetValue(declaringType, out var supportedMethodsForDeclaringType) && supportedMethodsForDeclaringType.Count > 0) | ||
{ | ||
return supportedMethodsForDeclaringType.Contains(member); | ||
} | ||
|
||
// Last resort, check if the method name is supported for object | ||
if (_supported.TryGetValue(typeof(object), out var supportedMethodsForObject) && supportedMethodsForObject.Count > 0) | ||
{ | ||
return supportedMethodsForObject.Any(x => x.Name == member.Name); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private void TryAdd(Type type, MethodInfo? method) | ||
{ | ||
if (method != null) | ||
{ | ||
_supported[type].Add(method); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.