Set eligibility to not run against (built-in) methods of record classes or structs

I've figured out how to set up an If statement for the record types, but I cannot figure out how to end it - how can I ensure my OverrideMethodAspect doesn't actually apply to any methods that are attached to a record? Even better though - is there any way to have it not apply to any methods that aren't automatically a part of a record (e.g. don't apply to ToString or Equals, but do apply to MyCustomMethod without specifically calling such custom methods out)? Thanks!
1 Reply
Petr Onderka
Petr Onderka17mo ago
builder.If is useful when you have a rule that you want to check only if some condition is true. E.g. something like:
builder.If(m => m.DeclaringType.TypeKind is TypeKind.RecordClass or TypeKind.RecordStruct)
.MustSatisfy(m => m.Name != nameof(ToString), m => $"record method can't be ToString");
builder.If(m => m.DeclaringType.TypeKind is TypeKind.RecordClass or TypeKind.RecordStruct)
.MustSatisfy(m => m.Name != nameof(ToString), m => $"record method can't be ToString");
Except this does not work correctly right now: https://github.com/postsharp/Metalama/issues/188. But if you just want to exclude all methods on a record, you don't need If:
builder.MustSatisfy(
m => m.DeclaringType.TypeKind is not (TypeKind.RecordClass or TypeKind.RecordStruct),
m => $"method can't be member of a record");
builder.MustSatisfy(
m => m.DeclaringType.TypeKind is not (TypeKind.RecordClass or TypeKind.RecordStruct),
m => $"method can't be member of a record");
If you only want to apply an aspect to methods that are declared in source code, you can use build.MustBeExplicitlyDeclared() for that. Though that is already included for OverrideMethodAspect (assuming you call base.BuildEligibility(builder);).
Want results from more Discord servers?
Add your server