Skip to content

winseros/ToStringDotNet

Repository files navigation

ToStringDotNet

The library purpose is to save you from handrwiting ToString() method implementations for your objects.

Instead of:

class MyClass
{
    public string MyProperty { get; set; }

    public void ToString()
    {
        var sb = new StringBuilder();
        sb.Append("{\"MyProperty\":\"");
        sb.Append(this.MyProperty);
        sb.Append("\"}");
        return sb.ToString();
    }
}

You can write:

class MyClass
{
    [ToString]
    public string MyProperty { get; set; }

    public void ToString()
    {
        return ToStringFormatter.Format(this);
    }
}

How to

Print an object

class MyClass
{
    [ToString]
    public string MyProperty1 { get; set; }

    [ToString]
    public int MyProperty2 { get; set; }
}

...

var obj = new MyObject{MyProperty1 = "p1", MyProperty2 = 1};
var str = ToStringFormatter.Format(obj);
Console.WriteLine(str);
//{"MyProperty1":"p1","MyProperty2":1}

Reorder properties

class MyClass
{
    [ToString(1)] //lower priority - goes last
    public string MyProperty1 { get; set; }

    [ToString(2)] //higher priority - goes first
    public int MyProperty2 { get; set; }
}

...

var obj = new MyObject{MyProperty1 = "p1", MyProperty2 = 1};
var str = ToStringFormatter.Format(obj);
Console.WriteLine(str);
//{"MyProperty2":1,"MyProperty1":"p2"}

Exclude inherited properties

class MyParentClass
{
    [ToString]
    public string MyProperty1 { get; set; }
}

[ToStringInheritance(ToStringInheritance.NotInherit)]
class MyChildClass: MyParentClass
{
    [ToString]
    public string MyProperty2 { get; set; }
}

...

var obj = new MyChildClass{MyProperty1 = "p1", MyProperty2 = "p2"};
var str = ToStringFormatter.Format(obj);
Console.WriteLine(str);
//{"MyProperty2":"p2"}

More examples

For more examples see the ToStringDotNet.Test project.

About

Will help you to avoid handwriting ToString methods for your .NET projects

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published