Skip to content

[Proposal]: PrintMembersIgnoreAttribute for records #3925

@Youssef1313

Description

@Youssef1313

PrintMembersIgnoreAttribute for records

  • Proposed
  • Prototype: Not Started
  • Implementation: Not Started
  • Specification: Not Started

Summary

Currently, there is no way for the developer to exclude specific fields or properties from record's PrintMembers.

Motivation

  1. 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.

  2. 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:

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class PrintMembersIgnoreAttribute : Attribute { }

If GenerateMethodBody in SynthesizedRecordPrintMembers found the field/property being decorated with this attribute, it shouldn't include it.

Drawbacks

Alternatives

Unresolved questions

  1. If this attribute is applied to non-record members, should the compiler warn and ignore it, or error it completely?
  2. Should the developer be able to force including statics and constants with a similar attribute (e.g. ForceIncludeInPrintMembersAttribute)?

Design meetings

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions