Skip to content
This repository has been archived by the owner on Dec 12, 2020. It is now read-only.

.NET Compiler Platform ("Roslyn") analyzer to disallow calling object.ToString() on types lacking an override.

License

Notifications You must be signed in to change notification settings

microsoft/Rosyln-Analyzer-ToStringWithoutOverride

Repository files navigation

Rosyln-Analyzer-ToStringWithoutOverride

.NET Compiler Platform ("Roslyn") analyzer to disallow calling object.ToString() on types lacking an override.

Motivation

Imagine we have the following simple POCO for representing money:

struct Money {
    public decimal amount;
    public string currency;
}

Then we try to print out an instance of it:

System.WriteLine("I need about {0}", new Money { amount = 3.50m, currency = "$" });

The statement will print I need about Money, which is not very useful. This came up a number of times on a team when writing code logging the state of objects for debugging purposes, leading to the creation of this analyzer.

After installing this analyzer the above WriteLine call will result in a compile-time error message Expression of type 'Money' will be implicitly converted to a string, but does not override ToString().

About

.NET Compiler Platform ("Roslyn") analyzer to disallow calling object.ToString() on types lacking an override.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published