Meaningful ToString()
is an extremely lightweight .NET class library that
helps describe your instances in a more meaningful way.
The default object.ToString()
isn't very useful when debugging or logging.
Meaningful ToString()
creates a string that looks something like:
"Employee (Id = 13298279, Title = Head of Sales, FirstName = Fred, LastName = Smith, Emails = System.String[], Manager = Employee (Id = 13298278, Title = Managing Director, FirstName = Joe, LastName = Bloggs, Emails = System.String[], Manager = null))"
And this is all that Meaningful ToString()
does!
Meaningful ToString()
is a .NET Standard 1.0 library, and can be used in the
following .NET environments:
- dotnet Core (CoreCLR) projects (versions 1 and up);
- full-fat .NET framework, v4.5 and beyond
-
Include the NuGet package:
Install-Package Meridian.MeaningfulToString
-
Alter your class to inherit from
Meridian.MeaningfulToString.MeaningfulBase
For example:
public class Employee : Meridian.MeaningfulToString.MeaningfulBase { public int Id { get; set; } // TODO: ... the rest of your class implementation here ... }
- Note 1: You can have multiple base classes - just as long as
ultimately, the class you want meaningful values for
ToString()
inherits fromMeaningfulBase
. - Note 2: This is actually not 100% required; this is just the quick
start. If you'd rather not inherit all your classes from this base
class, see below section "Without Inheriting from
MeaningfulBase
".
- Note 1: You can have multiple base classes - just as long as
ultimately, the class you want meaningful values for
-
That's it! Start debugging/logging!
You don't have to inherit from MeaningfulBase
if you don't want to. Simply
override
your class' implementation of ToString()
, and call the object
extension method MeaningfulToString()
:
public override string ToString()
{
return this.MeaningfulToString();
}
As with all extension methods, just remember to make sure to include the
Meridian.MeaningfulToString
namespace in your usings
:
using Meridian.MeaningfulToString;