-
Notifications
You must be signed in to change notification settings - Fork 30
Feature/optimize resolver #182
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 all commits
Commits
Show all changes
3 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
251 changes: 126 additions & 125 deletions
251
src/EntityFrameworkCore.Projectables/Extensions/TypeExtensions.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,168 +1,169 @@ | ||
| using System.Reflection; | ||
|
|
||
| namespace EntityFrameworkCore.Projectables.Extensions | ||
| namespace EntityFrameworkCore.Projectables.Extensions; | ||
|
|
||
| public static class TypeExtensions | ||
| { | ||
| public static class TypeExtensions | ||
| public static Type[] GetNestedTypePath(this Type type) | ||
| { | ||
| public static string GetSimplifiedTypeName(this Type type) | ||
| // First pass: count the nesting depth so we can size the array exactly. | ||
| var depth = 0; | ||
| var current = type; | ||
| while (true) | ||
| { | ||
| var name = type.Name; | ||
|
|
||
| var backtickIndex = name.IndexOf("`"); | ||
| if (backtickIndex != -1) | ||
| depth++; | ||
| if (!current.IsNested || current.DeclaringType is null) | ||
| { | ||
| name = name.Substring(0, backtickIndex); | ||
| break; | ||
| } | ||
|
|
||
| return name; | ||
| current = current.DeclaringType; | ||
| } | ||
|
|
||
| public static IEnumerable<Type> GetNestedTypePath(this Type type) | ||
| // Second pass: fill the array outermost-first by walking back from the leaf. | ||
| var path = new Type[depth]; | ||
| current = type; | ||
| for (var i = depth - 1; i >= 0; i--) | ||
| { | ||
| if (type.IsNested && type.DeclaringType is not null) | ||
| { | ||
| foreach (var containingType in type.DeclaringType.GetNestedTypePath()) | ||
| { | ||
| yield return containingType; | ||
| } | ||
| } | ||
|
|
||
| yield return type; | ||
| path[i] = current; | ||
| current = current.DeclaringType!; | ||
| } | ||
|
|
||
| private static bool CanHaveOverridingMethod(this Type derivedType, MethodInfo methodInfo) | ||
| { | ||
| // We only need to search for virtual instance methods who are not declared on the derivedType | ||
| if (derivedType == methodInfo.DeclaringType || methodInfo.IsStatic || !methodInfo.IsVirtual) | ||
| { | ||
| return false; | ||
| } | ||
| return path; | ||
| } | ||
|
|
||
| if (!derivedType.IsAssignableTo(methodInfo.DeclaringType)) | ||
| { | ||
| throw new ArgumentException("MethodInfo needs to be declared on the type hierarchy", nameof(methodInfo)); | ||
| } | ||
| private static bool CanHaveOverridingMethod(this Type derivedType, MethodInfo methodInfo) | ||
| { | ||
| // We only need to search for virtual instance methods who are not declared on the derivedType | ||
| if (derivedType == methodInfo.DeclaringType || methodInfo.IsStatic || !methodInfo.IsVirtual) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| if (!derivedType.IsAssignableTo(methodInfo.DeclaringType)) | ||
| { | ||
| throw new ArgumentException("MethodInfo needs to be declared on the type hierarchy", nameof(methodInfo)); | ||
| } | ||
|
|
||
| private static bool IsOverridingMethodOf(this MethodInfo methodInfo, MethodInfo baseDefinition) | ||
| => methodInfo.GetBaseDefinition() == baseDefinition; | ||
| return true; | ||
| } | ||
|
|
||
| private static bool IsOverridingMethodOf(this MethodInfo methodInfo, MethodInfo baseDefinition) | ||
| => methodInfo.GetBaseDefinition() == baseDefinition; | ||
|
|
||
| public static MethodInfo GetOverridingMethod(this Type derivedType, MethodInfo methodInfo) | ||
| public static MethodInfo GetOverridingMethod(this Type derivedType, MethodInfo methodInfo) | ||
| { | ||
| if (!derivedType.CanHaveOverridingMethod(methodInfo)) | ||
| { | ||
| if (!derivedType.CanHaveOverridingMethod(methodInfo)) | ||
| { | ||
| return methodInfo; | ||
| } | ||
| return methodInfo; | ||
| } | ||
|
|
||
| var derivedMethods = derivedType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); | ||
| var derivedMethods = derivedType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); | ||
|
|
||
| MethodInfo? overridingMethod = null; | ||
| if (derivedMethods is { Length: > 0 }) | ||
| { | ||
| var baseDefinition = methodInfo.GetBaseDefinition(); | ||
| overridingMethod = derivedMethods.FirstOrDefault(derivedMethodInfo | ||
| => derivedMethodInfo.IsOverridingMethodOf(baseDefinition)); | ||
| } | ||
|
|
||
| return overridingMethod ?? methodInfo; // If no derived methods were found, return the original methodInfo | ||
| MethodInfo? overridingMethod = null; | ||
| if (derivedMethods is { Length: > 0 }) | ||
| { | ||
| var baseDefinition = methodInfo.GetBaseDefinition(); | ||
| overridingMethod = derivedMethods.FirstOrDefault(derivedMethodInfo | ||
| => derivedMethodInfo.IsOverridingMethodOf(baseDefinition)); | ||
| } | ||
|
|
||
| public static PropertyInfo GetOverridingProperty(this Type derivedType, PropertyInfo propertyInfo) | ||
| return overridingMethod ?? methodInfo; // If no derived methods were found, return the original methodInfo | ||
| } | ||
|
|
||
| private static PropertyInfo GetOverridingProperty(this Type derivedType, PropertyInfo propertyInfo) | ||
| { | ||
| var accessor = propertyInfo.GetAccessors(true).FirstOrDefault(derivedType.CanHaveOverridingMethod); | ||
| if (accessor is null) | ||
| { | ||
| var accessor = propertyInfo.GetAccessors(true).FirstOrDefault(derivedType.CanHaveOverridingMethod); | ||
| if (accessor is null) | ||
| { | ||
| return propertyInfo; | ||
| } | ||
| return propertyInfo; | ||
| } | ||
|
|
||
| var isGetAccessor = propertyInfo.GetMethod == accessor; | ||
| var isGetAccessor = propertyInfo.GetMethod == accessor; | ||
|
|
||
| var derivedProperties = derivedType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); | ||
|
|
||
| PropertyInfo? overridingProperty = null; | ||
| if (derivedProperties is { Length: > 0 }) | ||
| { | ||
| var baseDefinition = accessor.GetBaseDefinition(); | ||
| overridingProperty = derivedProperties.FirstOrDefault(p | ||
| => (isGetAccessor ? p.GetMethod : p.SetMethod)?.IsOverridingMethodOf(baseDefinition) == true); | ||
| } | ||
|
|
||
| return overridingProperty ?? propertyInfo; // If no derived methods were found, return the original methodInfo | ||
| } | ||
| var derivedProperties = derivedType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); | ||
|
|
||
| public static MethodInfo GetImplementingMethod(this Type derivedType, MethodInfo methodInfo) | ||
| PropertyInfo? overridingProperty = null; | ||
| if (derivedProperties is { Length: > 0 }) | ||
| { | ||
| var interfaceType = methodInfo.DeclaringType; | ||
| // We only need to search for interface methods | ||
| if (interfaceType?.IsInterface != true || derivedType.IsInterface || methodInfo.IsStatic || !methodInfo.IsVirtual) | ||
| { | ||
| return methodInfo; | ||
| } | ||
|
|
||
| if (!derivedType.IsAssignableTo(interfaceType)) | ||
| { | ||
| throw new ArgumentException("MethodInfo needs to be declared on the type hierarchy", nameof(methodInfo)); | ||
| } | ||
| var baseDefinition = accessor.GetBaseDefinition(); | ||
| overridingProperty = derivedProperties.FirstOrDefault(p | ||
| => (isGetAccessor ? p.GetMethod : p.SetMethod)?.IsOverridingMethodOf(baseDefinition) == true); | ||
| } | ||
|
|
||
| var interfaceMap = derivedType.GetInterfaceMap(interfaceType); | ||
| for (var i = 0; i < interfaceMap.InterfaceMethods.Length; i++) | ||
| { | ||
| if (interfaceMap.InterfaceMethods[i] == methodInfo) | ||
| { | ||
| return interfaceMap.TargetMethods[i]; | ||
| } | ||
| } | ||
| return overridingProperty ?? propertyInfo; // If no derived methods were found, return the original methodInfo | ||
| } | ||
|
|
||
| throw new ApplicationException( | ||
| $"The interface map for {derivedType} doesn't contain the implemented method for {methodInfo}!"); | ||
| private static MethodInfo GetImplementingMethod(this Type derivedType, MethodInfo methodInfo) | ||
| { | ||
| var interfaceType = methodInfo.DeclaringType; | ||
| // We only need to search for interface methods | ||
| if (interfaceType?.IsInterface != true || derivedType.IsInterface || methodInfo.IsStatic || !methodInfo.IsVirtual) | ||
| { | ||
| return methodInfo; | ||
| } | ||
|
|
||
| public static PropertyInfo GetImplementingProperty(this Type derivedType, PropertyInfo propertyInfo) | ||
| if (!derivedType.IsAssignableTo(interfaceType)) | ||
| { | ||
| var accessor = propertyInfo.GetAccessors()[0]; | ||
| throw new ArgumentException("MethodInfo needs to be declared on the type hierarchy", nameof(methodInfo)); | ||
| } | ||
|
|
||
| var implementingAccessor = derivedType.GetImplementingMethod(accessor); | ||
| if (implementingAccessor == accessor) | ||
| var interfaceMap = derivedType.GetInterfaceMap(interfaceType); | ||
| for (var i = 0; i < interfaceMap.InterfaceMethods.Length; i++) | ||
| { | ||
| if (interfaceMap.InterfaceMethods[i] == methodInfo) | ||
| { | ||
| return propertyInfo; | ||
| return interfaceMap.TargetMethods[i]; | ||
| } | ||
| } | ||
|
|
||
| var implementingType = implementingAccessor.DeclaringType | ||
| // This should only be null if it is a property accessor on the global module, | ||
| // which should never happen since we found it from derivedType | ||
| ?? throw new ApplicationException("The property accessor has no declaring type!"); | ||
| throw new ApplicationException( | ||
| $"The interface map for {derivedType} doesn't contain the implemented method for {methodInfo}!"); | ||
| } | ||
|
|
||
| var derivedProperties = implementingType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); | ||
| public static PropertyInfo GetImplementingProperty(this Type derivedType, PropertyInfo propertyInfo) | ||
| { | ||
| var accessor = propertyInfo.GetAccessors()[0]; | ||
|
|
||
| return derivedProperties.FirstOrDefault(propertyInfo.GetMethod == accessor | ||
| ? p => MethodInfosEqual(p.GetMethod, implementingAccessor) | ||
| : p => MethodInfosEqual(p.SetMethod, implementingAccessor)) ?? propertyInfo; | ||
| var implementingAccessor = derivedType.GetImplementingMethod(accessor); | ||
| if (implementingAccessor == accessor) | ||
| { | ||
| return propertyInfo; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The built-in <see cref="MethodInfo.op_Equality(System.Reflection.MethodInfo?,System.Reflection.MethodInfo?)"/> | ||
| /// does not work if the <see cref="MemberInfo.ReflectedType"/>s don't agree. | ||
| /// </summary> | ||
| private static bool MethodInfosEqual(MethodInfo? first, MethodInfo second) | ||
| => first?.ReflectedType == second.ReflectedType | ||
| ? first == second | ||
| : first is not null | ||
| && first.DeclaringType == second.DeclaringType | ||
| && first.Name == second.Name | ||
| && first.GetParameters().Select(p => p.ParameterType) | ||
| .SequenceEqual(second.GetParameters().Select(p => p.ParameterType)) | ||
| && first.GetGenericArguments().SequenceEqual(second.GetGenericArguments()); | ||
|
|
||
| public static MethodInfo GetConcreteMethod(this Type derivedType, MethodInfo methodInfo) | ||
| => methodInfo.DeclaringType?.IsInterface == true | ||
| ? derivedType.GetImplementingMethod(methodInfo) | ||
| : derivedType.GetOverridingMethod(methodInfo); | ||
|
|
||
| public static PropertyInfo GetConcreteProperty(this Type derivedType, PropertyInfo propertyInfo) | ||
| => propertyInfo.DeclaringType?.IsInterface == true | ||
| ? derivedType.GetImplementingProperty(propertyInfo) | ||
| : derivedType.GetOverridingProperty(propertyInfo); | ||
| var implementingType = implementingAccessor.DeclaringType | ||
| // This should only be null if it is a property accessor on the global module, | ||
| // which should never happen since we found it from derivedType | ||
| ?? throw new ApplicationException("The property accessor has no declaring type!"); | ||
|
|
||
| var derivedProperties = implementingType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); | ||
|
|
||
| return derivedProperties.FirstOrDefault(propertyInfo.GetMethod == accessor | ||
| ? p => MethodInfosEqual(p.GetMethod, implementingAccessor) | ||
| : p => MethodInfosEqual(p.SetMethod, implementingAccessor)) ?? propertyInfo; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The built-in <see cref="MethodInfo.op_Equality(System.Reflection.MethodInfo?,System.Reflection.MethodInfo?)"/> | ||
| /// does not work if the <see cref="MemberInfo.ReflectedType"/>s don't agree. | ||
| /// </summary> | ||
| private static bool MethodInfosEqual(MethodInfo? first, MethodInfo second) | ||
| => first?.ReflectedType == second.ReflectedType | ||
| ? first == second | ||
| : first is not null | ||
| && first.DeclaringType == second.DeclaringType | ||
| && first.Name == second.Name | ||
| && first.GetParameters().Select(p => p.ParameterType) | ||
| .SequenceEqual(second.GetParameters().Select(p => p.ParameterType)) | ||
| && first.GetGenericArguments().SequenceEqual(second.GetGenericArguments()); | ||
|
|
||
| public static MethodInfo GetConcreteMethod(this Type derivedType, MethodInfo methodInfo) | ||
| => methodInfo.DeclaringType?.IsInterface == true | ||
| ? derivedType.GetImplementingMethod(methodInfo) | ||
| : derivedType.GetOverridingMethod(methodInfo); | ||
|
|
||
| public static PropertyInfo GetConcreteProperty(this Type derivedType, PropertyInfo propertyInfo) | ||
| => propertyInfo.DeclaringType?.IsInterface == true | ||
| ? derivedType.GetImplementingProperty(propertyInfo) | ||
| : derivedType.GetOverridingProperty(propertyInfo); | ||
| } |
10 changes: 5 additions & 5 deletions
10
src/EntityFrameworkCore.Projectables/Services/IProjectionExpressionResolver.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,10 +1,10 @@ | ||
| using System.Linq.Expressions; | ||
| using System.Reflection; | ||
|
|
||
| namespace EntityFrameworkCore.Projectables.Services | ||
| namespace EntityFrameworkCore.Projectables.Services; | ||
|
|
||
| public interface IProjectionExpressionResolver | ||
| { | ||
| public interface IProjectionExpressionResolver | ||
| { | ||
| LambdaExpression FindGeneratedExpression(MemberInfo projectableMemberInfo); | ||
| } | ||
| LambdaExpression FindGeneratedExpression(MemberInfo projectableMemberInfo, | ||
| ProjectableAttribute? projectableAttribute = null); | ||
| } | ||
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.