Code Monkey home page Code Monkey logo

printmembersgenerator's Introduction

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.

For more information about source generators, see Introducing C# source generators blog.

Overview

C# 9.0 introduced a new language feature called 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:

  <ItemGroup>
    <ProjectReference Include="PATH TO PrintMembersGenerator.csproj" 
                      OutputItemType="Analyzer"
                      ReferenceOutputAssembly="false" />
  </ItemGroup>

Use the attribute

First of all, here is the internal definition of the attribute:

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:

[property: PrintMembers(ShouldInclude = false)]

See this comment on dotnet/csharplang#3644.

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.

printmembersgenerator's People

Contributors

youssef1313 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

printmembersgenerator's Issues

Nuget package

Are there any plans to provide this is a nuget package?

Add a license

This is currently missing a license, could you add one?

Included in reference list

Hi,

I've included your repository in my personal C# Source Generators list to help gather these kind of projects in one place and hopefully under single umbrella, thus I propose to add the csharp-sourcegenerator topic to this repository to help with visibility.

I hope you're okay with this. If you happen to know other generator projects, I'll be happy to enlist those in my list as well.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.