Skip to content

Commit 28dac23

Browse files
committed
Simplify DynamicDataOperations: use FlattenHierarchy instead of manual hierarchy walk
Replace the manual type hierarchy walk with DeclaredOnly + loop with a single reflection call using BindingFlags.FlattenHierarchy. This lets the .NET runtime handle inheritance search for static members with its own built-in caching, removing the need for custom ConcurrentDictionary caches and the TypeMemberKey struct.
1 parent ad877cd commit 28dac23

File tree

1 file changed

+4
-49
lines changed

1 file changed

+4
-49
lines changed

src/TestFramework/TestFramework/Attributes/DataSource/DynamicDataOperations.cs

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting;
55

66
internal static class DynamicDataOperations
77
{
8-
private const BindingFlags DeclaredOnlyLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
8+
private const BindingFlags MemberLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy;
99

1010
public static IEnumerable<object[]> GetData(Type? dynamicDataDeclaringType, DynamicDataSourceType dynamicDataSourceType, string dynamicDataSourceName, object?[] dynamicDataSourceArguments, MethodInfo methodInfo)
1111
{
@@ -167,56 +167,11 @@ private static bool TryGetData(object dataSource, [NotNullWhen(true)] out IEnume
167167
}
168168

169169
private static FieldInfo? GetFieldConsideringInheritance(Type type, string fieldName)
170-
{
171-
// NOTE: Don't use GetRuntimeField. It considers inheritance only for instance fields.
172-
Type? currentType = type;
173-
while (currentType is not null)
174-
{
175-
FieldInfo? field = currentType.GetField(fieldName, DeclaredOnlyLookup);
176-
if (field is not null)
177-
{
178-
return field;
179-
}
180-
181-
currentType = currentType.BaseType;
182-
}
183-
184-
return null;
185-
}
170+
=> type.GetField(fieldName, MemberLookup);
186171

187172
private static PropertyInfo? GetPropertyConsideringInheritance(Type type, string propertyName)
188-
{
189-
// NOTE: Don't use GetRuntimeProperty. It considers inheritance only for instance properties.
190-
Type? currentType = type;
191-
while (currentType is not null)
192-
{
193-
PropertyInfo? property = currentType.GetProperty(propertyName, DeclaredOnlyLookup);
194-
if (property is not null)
195-
{
196-
return property;
197-
}
198-
199-
currentType = currentType.BaseType;
200-
}
201-
202-
return null;
203-
}
173+
=> type.GetProperty(propertyName, MemberLookup);
204174

205175
private static MethodInfo? GetMethodConsideringInheritance(Type type, string methodName)
206-
{
207-
// NOTE: Don't use GetRuntimeMethod. It considers inheritance only for instance methods.
208-
Type? currentType = type;
209-
while (currentType is not null)
210-
{
211-
MethodInfo? method = currentType.GetMethod(methodName, DeclaredOnlyLookup);
212-
if (method is not null)
213-
{
214-
return method;
215-
}
216-
217-
currentType = currentType.BaseType;
218-
}
219-
220-
return null;
221-
}
176+
=> type.GetMethod(methodName, MemberLookup);
222177
}

0 commit comments

Comments
 (0)