Skip to content

Commit b0ba64d

Browse files
committed
Exclude record members from TypeAccessor
The C# compiler automatically adds a number of members to `record` types, such as an `EqualityContract` and `ToString()`. These aren't relevant to either dispatch or mapping operations, and just add noise to the cache. These can be excluded. To ensure this works, I've converted `CustomTopicTopicViewModel` (5310655) to a `record` so its `MaybeCompatible` checks can validate whether there are any unexpected members. While I was at it, I also consolidated all of the escape clauses in `IsValid()` into a single block for readability and succinctness.
1 parent a68d459 commit b0ba64d

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

OnTopic.Tests/ViewModels/CustomTopicTopicViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace OnTopic.Tests.ViewModels {
2727
/// of the test case, and not something we'd expect in real-life scenarios.
2828
/// </para>
2929
/// </remarks>
30-
public class CustomTopicTopicViewModel {
30+
public record CustomTopicTopicViewModel {
3131

3232
/*==========================================================================================================================
3333
| CONSTRUCTOR

OnTopic/Internal/Reflection/MemberAccessor.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,21 @@ internal void Validate(object target) {
233233
}
234234
}
235235

236+
/*==========================================================================================================================
237+
| EXCLUDED MEMBERS
238+
\-------------------------------------------------------------------------------------------------------------------------*/
239+
/// <summary>
240+
/// Provides a list of member names automatically generated by the compiler for <c>record</c> types, but which aren't
241+
/// relevant to mapping and should be excluded.
242+
/// </summary>
243+
private static List<string> ExcludedMembers { get; } = new List<string>() {
244+
"EqualityContract",
245+
"GetHashCode",
246+
"ToString",
247+
"<Clone>$"
248+
};
249+
250+
236251
/*==========================================================================================================================
237252
| IS VALID?
238253
\-------------------------------------------------------------------------------------------------------------------------*/
@@ -247,23 +262,20 @@ internal void Validate(object target) {
247262
internal static Func<MemberInfo, bool> IsValid => memberInfo => {
248263

249264
/*------------------------------------------------------------------------------------------------------------------------
250-
| Skip members inherited from object
265+
| Exclude unsupported types and members
251266
\-----------------------------------------------------------------------------------------------------------------------*/
252267
if (memberInfo.DeclaringType == typeof(object)) return false;
253-
254-
/*------------------------------------------------------------------------------------------------------------------------
255-
| Ensure type is property or method
256-
\-----------------------------------------------------------------------------------------------------------------------*/
257268
if (memberInfo is not MethodInfo and not PropertyInfo) return false;
269+
if (memberInfo.Name.Contains("et_", StringComparison.Ordinal)) return false;
270+
if (ExcludedMembers.Contains(memberInfo.Name)) return false;
258271

259272
/*------------------------------------------------------------------------------------------------------------------------
260273
| Validate properties
261274
\-----------------------------------------------------------------------------------------------------------------------*/
262-
if (memberInfo.Name.Contains("et_", StringComparison.Ordinal)) return false;
263275
if (memberInfo is PropertyInfo) return true;
264276

265277
/*------------------------------------------------------------------------------------------------------------------------
266-
| Validate methods
278+
| Establish method variables
267279
\-----------------------------------------------------------------------------------------------------------------------*/
268280
var methodInfo = (MethodInfo)memberInfo;
269281
var parameters = methodInfo.GetParameters();

0 commit comments

Comments
 (0)