Skip to content

ToStringGenerator: Add record type support #146

@BoBoBaSs84

Description

@BoBoBaSs84

Is your feature request related to a problem? Please describe.
C# records already generate a PrintMembers method and ToString() override, but their output format is fixed by the compiler. Users who want a different format (matching the ClassName { Prop = val } style used by this generator) or who want to exclude specific properties from the record's ToString() cannot use the generator today since it only targets ClassDeclarationSyntax.

Describe the solution you'd like
Support record and record struct declarations by:

  • Allowing GenerateToStringAttribute on record types (AttributeTargets.Class | AttributeTargets.Struct).
  • Detecting RecordDeclarationSyntax in the pipeline.
  • Generating a sealed override of PrintMembers(StringBuilder) or a direct ToString() override as appropriate.

Describe alternatives you've considered

  • Telling users to manually override PrintMembers, but this negates the generator's value.
  • Ignoring records since they already have a ToString(), but the exclusion and formatting features are still valuable.

Additional context
Records use PrintMembers internally. The generator should decide whether to override PrintMembers (preserving the record contract) or override ToString() directly (potentially with sealed to prevent further overrides in derived records).

Metadata

Metadata

Assignees

Labels

featureNew feature or request

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions