Open sandboxFocus

Method CanBeDeclaredExplicitly

CanBeDeclaredExplicitly(IMember)

Determines whether an explicit declaration of a member can be written in source code, which is a condition for an aspect to override the member.

Declaration
public static bool CanBeDeclaredExplicitly(this IMember member)
Parameters
Type Name Description
IMember member

The member to check.

Returns
Type Description
bool

true if an explicit declaration of the member can be written in source code; otherwise, false.

Remarks

The method returns false in two cases. The first case is a member that the C# compiler adds to a record even when the record declares this member itself: Equals(object), the Equals overload whose parameter is a base record, and the == and != operators. A declaration of one of these members is a duplicate, which the C# compiler reports as an error. The second case is a compiler-generated field, such as the backing field of an auto-property or the field that captures a primary constructor parameter, because the name of such a field is not a valid C# identifier. For an accessor, the method returns the same value as for the property or the event that contains the accessor.

The method returns true for any other member. In particular, it returns true for the record members that the C# compiler generates only when the record does not declare them: the Equals overload whose parameter is the declaring record, GetHashCode, ToString, PrintMembers, Deconstruct, EqualityContract and the copy constructor. When an aspect overrides one of these members, Proceed() runs the implementation that the C# compiler would have generated. The copy constructor is the exception: no advice can target it, because MustNotBeRecordCopyConstructor(IEligibilityBuilder<IConstructor>) excludes it from every advice that could.