ToString

A well-crafted ToString implementation speeds up debugging and log analysis. Typically, it should return a concise description of the object, highlighting its identity and state. With Metalama, you can skip writing this repetitive code manually.

Example

Consider the ToString example aspect, applied to a class. In this scenario, we adopt an opt-out approach, marking members with [NotToString] that we prefer to exclude.

[ToString]
internal class MovingVertex
{
    public double X { get; set; }

    public double Y { get; set; }

    public double DX { get; set; }

    public double DY { get; set; }

    [NotToString]
    public double Velocity => Math.Sqrt((this.DX * this.DX) + (this.DY * this.DY));
}

The aspect generates the following code:

public override string ToString()
{
    return $"{{MovingVertex DX={DX}, DY={DY}, X={X}, Y={Y}}}";
}

To modify the code generation pattern, adjust the aspect class itself, ToStringAttribute.

Resources