Skip to content
This repository was archived by the owner on Jun 10, 2023. It is now read-only.

postsharp/PostSharp.Community.ToString

Repository files navigation

  PostSharp.Community.ToString

Auto-generates ToString() based on your class's fields and/or properties.

This is an add-in for PostSharp. It modifies your assembly during compilation by using IL weaving.

Full Pipeline

Example

Your code:

[ToString]
class TestClass
{
    public int Foo { get; set; }
    public double bar;
    
    [IgnoreDuringToString]
    public string Baz { get; set; }
}

What gets compiled:

class TestClass
{
    public int Foo { get; set; }
    public double bar;    
    public string Baz { get; set; }
    
    public override string ToString()
    {
        return $"TestClass; Foo:{Foo},bar:{bar}";
    }
}

Installation

  1. Install the NuGet package: PM> Install-Package PostSharp.Community.ToString
  2. Get a free PostSharp Community license at https://www.postsharp.net/get/free
  3. When you compile for the first time, you'll be asked to enter the license key.

How to use

Add [ToString] to the classes where you want to generate ToString, or use multicasting.

You can also use [IgnoreDuringToString] to exclude some fields or properties from ToString.

You can also set some formatting options in the properties of the ToString attribute. You can also set these formatting options globally by adding the assembly-wide attribute [assembly:ToStringGlobalOptions].

Possible options are:

  • PropertiesSeparator
  • PropertyNameToValueSeparator
  • WriteTypeName
  • WrapWithBraces
  • IncludePrivate (includes also private members)
  • PropertyNamingConvention (CamelCase option)

ToString will include all non-private fields and properties from the class including accessible fields and properties from any base classes.

For arrays and collections, ToString will print the first four elements of the collection, separated by PropertiesSeparator (for example, [1,2,3,4,...] for an array of 5 or more integers).

If you want to exclude some fields or properties from ToString everywhere, for example, exclude everything that's protected, you can multicast the [IgnoreDuringToString] attribute like this:

[assembly: IgnoreDuringToString(AttributeTargetMemberAttributes = MulticastAttributes.Protected)

Copyright notices

Published under the MIT license.

About

Auto-generates ToString() based on your class's fields and/or properties.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages