vovgou / ToString

Generate ToString method from public properties.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ToString.Fody

Chat on Gitter NuGet Status

Generates ToString method from public properties for class decorated with a [ToString] Attribute.

This is an add-in for Fody

It is expected that all developers using Fody become a Patron on OpenCollective. See Licensing/Patron FAQ for more information.

Usage

See also Fody usage.

NuGet installation

Install the ToString.Fody NuGet package and update the Fody NuGet package:

PM> Install-Package Fody
PM> Install-Package ToString.Fody

The Install-Package Fody is required since NuGet always defaults to the oldest, and most buggy, version of any dependency.

Add to FodyWeavers.xml

Add <ToString/> to FodyWeavers.xml

<Weavers>
  <ToString/>
</Weavers>

Your Code

[ToString]
class TestClass
{
    public int Foo { get; set; }

    public double Bar { get; set; }
    
    [IgnoreDuringToString]
    public string Baz { get; set; }
}

What gets compiled

class TestClass
{
    public int Foo { get; set; }

    public double Bar { get; set; }
    
    public string Baz { get; set; }
    
    public override string ToString()
    {
        return string.Format(
            CultureInfo.InvariantCulture, 
            "{{T: TestClass, Foo: {0}, Bar: {1}}}",
            Foo,
            Bar);
    }
}

Options

PropertyNameToValueSeparator

Default: :

For example:

<Weavers>
  <ToString PropertyNameToValueSeparator="->"/>
</Weavers>

PropertiesSeparator

Default: ,

For example:

<Weavers>
  <ToString PropertiesSeparator=". "/>
</Weavers>

WrapWithBrackets

Default: true

For example:

<Weavers>
  <ToString WrapWithBrackets="false"/>
</Weavers>

WriteTypeName

Default: true

For example:

<Weavers>
  <ToString WriteTypeName="false"/>
</Weavers>

ListStart

Default: [

For example:

<Weavers>
  <ToString ListStart="("/>
</Weavers>

ListEnd

Default: ]

For example:

<Weavers>
  <ToString ListEnd=")"/>
</Weavers>

Icon

Icon courtesy of The Noun Project

About

Generate ToString method from public properties.

License:MIT License


Languages

Language:C# 100.0%