-
Notifications
You must be signed in to change notification settings - Fork 292
Cache reflection lookups in DynamicDataOperations #7535
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |||
|
|
||||
| internal static class DynamicDataOperations | ||||
| { | ||||
| private const BindingFlags DeclaredOnlyLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly; | ||||
| private const BindingFlags MemberLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy; | ||||
|
||||
|
|
||||
| public static IEnumerable<object[]> GetData(Type? dynamicDataDeclaringType, DynamicDataSourceType dynamicDataSourceType, string dynamicDataSourceName, object?[] dynamicDataSourceArguments, MethodInfo methodInfo) | ||||
| { | ||||
|
|
@@ -19,15 +19,15 @@ | |||
| { | ||||
| case DynamicDataSourceType.AutoDetect: | ||||
| #pragma warning disable IDE0045 // Convert to conditional expression - it becomes less readable. | ||||
| if (GetPropertyConsideringInheritance(dynamicDataDeclaringType, dynamicDataSourceName) is { } dynamicDataPropertyInfo) | ||||
| if (dynamicDataDeclaringType.GetProperty(dynamicDataSourceName, MemberLookup) is { } dynamicDataPropertyInfo) | ||||
| { | ||||
| obj = GetDataFromProperty(dynamicDataPropertyInfo); | ||||
| } | ||||
| else if (GetMethodConsideringInheritance(dynamicDataDeclaringType, dynamicDataSourceName) is { } dynamicDataMethodInfo) | ||||
| else if (dynamicDataDeclaringType.GetMethod(dynamicDataSourceName, MemberLookup) is { } dynamicDataMethodInfo) | ||||
| { | ||||
| obj = GetDataFromMethod(dynamicDataMethodInfo, dynamicDataSourceArguments); | ||||
| } | ||||
| else if (GetFieldConsideringInheritance(dynamicDataDeclaringType, dynamicDataSourceName) is { } dynamicDataFieldInfo) | ||||
| else if (dynamicDataDeclaringType.GetField(dynamicDataSourceName, MemberLookup) is { } dynamicDataFieldInfo) | ||||
| { | ||||
| obj = GetDataFromField(dynamicDataFieldInfo); | ||||
| } | ||||
|
|
@@ -39,21 +39,21 @@ | |||
|
|
||||
| break; | ||||
| case DynamicDataSourceType.Property: | ||||
| PropertyInfo property = GetPropertyConsideringInheritance(dynamicDataDeclaringType, dynamicDataSourceName) | ||||
| PropertyInfo property = dynamicDataDeclaringType.GetProperty(dynamicDataSourceName, MemberLookup) | ||||
| ?? throw new ArgumentNullException($"{DynamicDataSourceType.Property} {dynamicDataSourceName}"); | ||||
|
|
||||
| obj = GetDataFromProperty(property); | ||||
| break; | ||||
|
|
||||
| case DynamicDataSourceType.Method: | ||||
| MethodInfo method = GetMethodConsideringInheritance(dynamicDataDeclaringType, dynamicDataSourceName) | ||||
| MethodInfo method = dynamicDataDeclaringType.GetMethod(dynamicDataSourceName, MemberLookup) | ||||
| ?? throw new ArgumentNullException($"{DynamicDataSourceType.Method} {dynamicDataSourceName}"); | ||||
|
|
||||
| obj = GetDataFromMethod(method, dynamicDataSourceArguments); | ||||
| break; | ||||
|
|
||||
| case DynamicDataSourceType.Field: | ||||
| FieldInfo field = GetFieldConsideringInheritance(dynamicDataDeclaringType, dynamicDataSourceName) | ||||
| FieldInfo field = dynamicDataDeclaringType.GetField(dynamicDataSourceName, MemberLookup) | ||||
| ?? throw new ArgumentNullException($"{DynamicDataSourceType.Field} {dynamicDataSourceName}"); | ||||
|
|
||||
| obj = GetDataFromField(field); | ||||
|
|
@@ -166,57 +166,4 @@ | |||
| return false; | ||||
| } | ||||
|
|
||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| private static FieldInfo? GetFieldConsideringInheritance(Type type, string fieldName) | ||||
| { | ||||
| // NOTE: Don't use GetRuntimeField. It considers inheritance only for instance fields. | ||||
| Type? currentType = type; | ||||
| while (currentType is not null) | ||||
| { | ||||
| FieldInfo? field = currentType.GetField(fieldName, DeclaredOnlyLookup); | ||||
| if (field is not null) | ||||
| { | ||||
| return field; | ||||
| } | ||||
|
|
||||
| currentType = currentType.BaseType; | ||||
| } | ||||
|
|
||||
| return null; | ||||
| } | ||||
|
|
||||
| private static PropertyInfo? GetPropertyConsideringInheritance(Type type, string propertyName) | ||||
| { | ||||
| // NOTE: Don't use GetRuntimeProperty. It considers inheritance only for instance properties. | ||||
| Type? currentType = type; | ||||
| while (currentType is not null) | ||||
| { | ||||
| PropertyInfo? property = currentType.GetProperty(propertyName, DeclaredOnlyLookup); | ||||
| if (property is not null) | ||||
| { | ||||
| return property; | ||||
| } | ||||
|
|
||||
| currentType = currentType.BaseType; | ||||
| } | ||||
|
|
||||
| return null; | ||||
| } | ||||
|
|
||||
| private static MethodInfo? GetMethodConsideringInheritance(Type type, string methodName) | ||||
| { | ||||
| // NOTE: Don't use GetRuntimeMethod. It considers inheritance only for instance methods. | ||||
| Type? currentType = type; | ||||
| while (currentType is not null) | ||||
| { | ||||
| MethodInfo? method = currentType.GetMethod(methodName, DeclaredOnlyLookup); | ||||
| if (method is not null) | ||||
| { | ||||
| return method; | ||||
| } | ||||
|
|
||||
| currentType = currentType.BaseType; | ||||
| } | ||||
|
|
||||
| return null; | ||||
| } | ||||
| } | ||||
|
Check failure on line 169 in src/TestFramework/TestFramework/Attributes/DataSource/DynamicDataOperations.cs
|
||||
Uh oh!
There was an error while loading. Please reload this page.