Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Youssef1313/PrintMembersGenerator
A source generator that helps re-defining C# record's PrintMembers method to force include/exclude certain members.
https://github.com/Youssef1313/PrintMembersGenerator
csharp csharp-sourcegenerator printmembers records source-generators
Last synced: 3 months ago
JSON representation
A source generator that helps re-defining C# record's PrintMembers method to force include/exclude certain members.
- Host: GitHub
- URL: https://github.com/Youssef1313/PrintMembersGenerator
- Owner: Youssef1313
- Created: 2020-09-24T21:37:42.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2020-09-25T21:59:54.000Z (about 4 years ago)
- Last Synced: 2024-05-01T16:27:24.717Z (6 months ago)
- Topics: csharp, csharp-sourcegenerator, printmembers, records, source-generators
- Language: C#
- Homepage:
- Size: 35.2 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- RSCG_Examples - PrintMembersGenerator
- csharp-source-generators - PrintMembersGenerator - ![stars](https://img.shields.io/github/stars/Youssef1313/PrintMembersGenerator?style=flat-square&cacheSeconds=604800) ![last commit](https://img.shields.io/github/last-commit/Youssef1313/PrintMembersGenerator?style=flat-square&cacheSeconds=86400) helps re-defining C# record's PrintMembers method to force include/exclude certain members. (Source Generators / Other)
README
# PrintMembersGenerator
A source generator that helps re-defining C# record's PrintMembers method to force include/exclude certain members.
This project is related to [dotnet/csharplang#3925](https://github.com/dotnet/csharplang/issues/3925).
For more information about source generators, see [Introducing C# source generators blog](https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/).
## Overview
C# 9.0 introduced a new language feature called [Records](https://docs.microsoft.com/dotnet/csharp/language-reference/proposals/csharp-9.0/records). A record is simply a reference type with some compiler-generated (synthesized) methods.
The interest of this project is on `PrintMembers` method, which the compiler uses when you call `.ToString()` on a record.
The compiler only includes all public non-override instance fields/properties in `PrintMembers`, and allows you to re-define PrintMembers if the provided implementation doesn't suite your need.
This source generator makes it easier to force-include or force-exclude certain record members in `PrintMembers` implementation through a provided attribute.
## How to use
### Add reference to the generator
Currently, this is not packed into a NuGet package. So you'll have to clone the source code, and add a reference to the *PrintMembersSourceGenerators.csproj*. The added reference should look like the following:
```xml
```### Use the attribute
First of all, here is the internal definition of the attribute:
```csharp
namespace SourceGenerator
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class PrintMembersAttribute : Attribute
{
public bool ShouldInclude { get; set; } = true;
}
}
```As you see, the attribute can only be applied to fields and properties.
- To force-include a member in `PrintMembers`, you should either use `[PrintMembers]` or `[PrintMembers(ShouldInclude = true)]`.
- To force-exclude a member in `PrintMembers`, you should use `[PrintMembers(ShouldInclude = false)]`.Currently, positional record parameters are not supported. Support for them should be added soon through targeted attributes:
```csharp
[property: PrintMembers(ShouldInclude = false)]
```See [this comment on dotnet/csharplang#3644](https://github.com/dotnet/csharplang/issues/3644#issuecomment-692742525).
## Notes
1. If the semantics of the added attribute is the **same** as what the compiler does, the generator ignores the attribute.
2. The record **must** be partial to allow the source generator to re-define `PrintMembers`.