-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[Proposal]: PrintMembersIgnoreAttribute for records #3925
Comments
If we were going to allow a user to force the inclusion of a member, we could just use one attribute. For example: [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class PrintMembersInclusion : Attribute
{
public PrintMembersInclusion(bool shouldInclude)
{
this.ShouldInclude = shouldInclude;
}
public bool ShouldInclude { get; }
} This could then be applied as follows, with the described behaviour: record TestRecord
{
[PrintMembersInclusion(shouldInclude: true)] // Included. Overrides default behaviour for constants.
public const string PublicConstantString = "This is a string.";
[PrintMembersInclusion(shouldInclude: true)] // Included. Overrides default behaviour for static readonly fields.
public static readonly int PublicStaticReadonlyInt = 42;
[PrintMembersInclusion(shouldInclude: false)] // No effect. Field is already ignored.
private static readonly int PrivateStaticReadonlyInt = 1788;
[PrintMembersInclusion(shouldInclude: true)] // No effect. Property is already included.
public string PublicInstanceString { get; }
[PrintMembersInclusion(shouldInclude: false)] // Not included. Overrides default behaviour for public properties.
public int PublicInstanceInt { get; }
[PrintMembersInclusion(shouldInclude: true)] // Included. Overrides default behaviour for private properties.
private bool PrivateBool { get; }
} |
Seems like something that could be handled by a source generator which emits the appropriate |
I would agree with @HaloFour's sentiment here. This, particularly the potential for stack overflows, was explicitly discussed, and we feel that the scenarios you're going to want to have a custom |
If anyone is interested, I started working on a simple source generator for this purpose. |
* We should Inform people about appropriate use of analyzers as per #3925 * Update README.md Co-authored-by: Fred Silberberg <fred@silberberg.xyz>
The generated
An attribute for excluding properties would be a convenient way to fix |
It's not in regard to equality, it is in regard with having policy drive the language's behavior. The compiler offers an escape hatch in that if you provide an implementation of |
PrintMembersIgnoreAttribute for records
Summary
Currently, there is no way for the developer to exclude specific fields or properties from record's PrintMembers.
Motivation
If a record contains an instance member of its same type, PrintMembers will get called recursively forever. Converting the instance member to be a static is one way to fix this, but being able to completely exclude an instance member seems to be a great addition.
If one of the properties doesn't have an overridden
ToString()
implementation. It's useless to be printed, the developer might want to exclude it, but in that case marking it as static wouldn't be always an available option.Detailed design
A new attribute is introduced:
If GenerateMethodBody in SynthesizedRecordPrintMembers found the field/property being decorated with this attribute, it shouldn't include it.
Drawbacks
Alternatives
Unresolved questions
ForceIncludeInPrintMembersAttribute
)?Design meetings
The text was updated successfully, but these errors were encountered: