ToString
A well-crafted ToString
implementation can significantly enhance your productivity during debugging and log analysis. Typically, it should return a concise description of the object, highlighting its identity and state. With Metalama, you can bypass writing this repetitive code manually.
Example
Let’s explore the ToString example aspect and apply the [ToString]
aspect 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}}}";
}
Modifying the code generation pattern is straightforward. Simply adjust the aspect class itself, ToStringAttribute
.
Resources
- Commented example: ToString