Skip to content

Commit aab4834

Browse files
author
Rhodon
committed
Fix interface property lookup in generic method
1 parent 3b98b1e commit aab4834

File tree

2 files changed

+67
-33
lines changed

2 files changed

+67
-33
lines changed

src/EntityFrameworkCore.Projectables/Extensions/TypeExtensions.cs

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,8 @@ private static bool CanHaveOverridingMethod(this Type derivedType, MethodInfo me
4141
return true;
4242
}
4343

44-
private static int? GetOverridingMethodIndex(this MethodInfo methodInfo, MethodInfo[]? allDerivedMethods)
45-
{
46-
if (allDerivedMethods is { Length: > 0 })
47-
{
48-
var baseDefinition = methodInfo.GetBaseDefinition();
49-
for (var i = 0; i < allDerivedMethods.Length; i++)
50-
{
51-
var derivedMethodInfo = allDerivedMethods[i];
52-
if (derivedMethodInfo.GetBaseDefinition() == baseDefinition)
53-
{
54-
return i;
55-
}
56-
}
57-
}
58-
59-
return null;
60-
}
44+
private static bool IsOverridingMethodOf(this MethodInfo methodInfo, MethodInfo baseDefinition)
45+
=> methodInfo.GetBaseDefinition() == baseDefinition;
6146

6247
public static MethodInfo GetOverridingMethod(this Type derivedType, MethodInfo methodInfo)
6348
{
@@ -68,31 +53,38 @@ public static MethodInfo GetOverridingMethod(this Type derivedType, MethodInfo m
6853

6954
var derivedMethods = derivedType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
7055

71-
return methodInfo.GetOverridingMethodIndex(derivedMethods) is { } i
72-
? derivedMethods[i]
73-
// No derived methods were found. Return the original methodInfo
74-
: methodInfo;
56+
MethodInfo? overridingMethod = null;
57+
if (derivedMethods is { Length: > 0 })
58+
{
59+
var baseDefinition = methodInfo.GetBaseDefinition();
60+
overridingMethod = derivedMethods.FirstOrDefault(derivedMethodInfo
61+
=> derivedMethodInfo.IsOverridingMethodOf(baseDefinition));
62+
}
63+
64+
return overridingMethod ?? methodInfo; // If no derived methods were found, return the original methodInfo
7565
}
7666

7767
public static PropertyInfo GetOverridingProperty(this Type derivedType, PropertyInfo propertyInfo)
7868
{
79-
var accessor = propertyInfo.GetAccessors()[0];
80-
81-
if (!derivedType.CanHaveOverridingMethod(accessor))
69+
var accessor = propertyInfo.GetAccessors(true).FirstOrDefault(derivedType.CanHaveOverridingMethod);
70+
if (accessor is null)
8271
{
8372
return propertyInfo;
8473
}
74+
75+
var isGetAccessor = propertyInfo.GetMethod == accessor;
8576

8677
var derivedProperties = derivedType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
87-
var derivedPropertyMethods = derivedProperties
88-
.Select((Func<PropertyInfo, MethodInfo?>)
89-
(propertyInfo.GetMethod == accessor ? p => p.GetMethod : p => p.SetMethod))
90-
.OfType<MethodInfo>().ToArray();
91-
92-
return accessor.GetOverridingMethodIndex(derivedPropertyMethods) is { } i
93-
? derivedProperties[i]
94-
// No derived methods were found. Return the original methodInfo
95-
: propertyInfo;
78+
79+
PropertyInfo? overridingProperty = null;
80+
if (derivedProperties is { Length: > 0 })
81+
{
82+
var baseDefinition = accessor.GetBaseDefinition();
83+
overridingProperty = derivedProperties.FirstOrDefault(p
84+
=> (isGetAccessor ? p.GetMethod : p.SetMethod)?.IsOverridingMethodOf(baseDefinition) == true);
85+
}
86+
87+
return overridingProperty ?? propertyInfo; // If no derived methods were found, return the original methodInfo
9688
}
9789

9890
public static MethodInfo GetImplementingMethod(this Type derivedType, MethodInfo methodInfo)

tests/EntityFrameworkCore.Projectables.FunctionalTests/InheritedModelTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,20 @@ namespace EntityFrameworkCore.Projectables.FunctionalTests
2020
[UsesVerify]
2121
public class InheritedModelTests
2222
{
23+
public interface IBaseProvider<TBase>
24+
{
25+
ICollection<TBase> Bases { get; set; }
26+
}
27+
28+
public class BaseProvider : IBaseProvider<Concrete>
29+
{
30+
public int Id { get; set; }
31+
public ICollection<Concrete> Bases { get; set; }
32+
}
33+
2334
public interface IBase
2435
{
36+
int Id { get; }
2537
int ComputedProperty { get; }
2638
int ComputedMethod();
2739
}
@@ -117,6 +129,26 @@ public Task ProjectOverImplementedMethod()
117129

118130
return Verifier.Verify(query.ToQueryString());
119131
}
132+
133+
[Fact]
134+
public Task ProjectOverProvider()
135+
{
136+
using var dbContext = new SampleDbContext<BaseProvider>();
137+
138+
var query = dbContext.Set<BaseProvider>().AllBases<BaseProvider, Concrete>();
139+
140+
return Verifier.Verify(query.ToQueryString());
141+
}
142+
143+
[Fact]
144+
public Task ProjectOverExtensionMethod()
145+
{
146+
using var dbContext = new SampleDbContext<Concrete>();
147+
148+
var query = dbContext.Set<Concrete>().Select(c => c.ComputedPropertyPlusMethod());
149+
150+
return Verifier.Verify(query.ToQueryString());
151+
}
120152
}
121153

122154
public static class ModelExtensions
@@ -128,5 +160,15 @@ public static IQueryable<int> SelectComputedProperty<TConcrete>(this IQueryable<
128160
public static IQueryable<int> SelectComputedMethod<TConcrete>(this IQueryable<TConcrete> concretes)
129161
where TConcrete : InheritedModelTests.IBase
130162
=> concretes.Select(x => x.ComputedMethod());
163+
164+
public static IQueryable<int> AllBases<TProvider, TBase>(this IQueryable<TProvider> concretes)
165+
where TProvider : InheritedModelTests.IBaseProvider<TBase>
166+
where TBase : InheritedModelTests.IBase
167+
=> concretes.SelectMany(x => x.Bases).Select(x => x.Id);
168+
169+
[Projectable]
170+
public static int ComputedPropertyPlusMethod<TConcrete>(this TConcrete concrete)
171+
where TConcrete : InheritedModelTests.IBase
172+
=> concrete.ComputedProperty + concrete.ComputedMethod();
131173
}
132174
}

0 commit comments

Comments
 (0)