| name | StructRecordsGenerator |
| nuget |
https://www.nuget.org/packages/StructRecordGenerator/ |
| link | https://github.com/SergeyTeplyakov/StructRecordsGenerator |
| author | Sergey Teplyakov |
This will generate code .ToString. Usefull for debugging
The code that you start with is
[StructGenerators.GenerateToString(PrintTypeName = true)]
class Person
{
[Required]
public string FirstName { get; set; }
public string LastName { get; set; }
}
The code that you will use is
var p = new Person();
p.FirstName = "Andrei";
//put here a debug watch to see p
Console.WriteLine(p.ToString());
The code that is generated is
partial class Person
{
/// <inheritdoc/>
public override string ToString()
{
var sb = new StringBuilder();
sb.Append("Person ");
sb.Append("{ ");
if (PrintMembers(sb))
{
sb.Append(" ");
}
sb.Append("}");
return sb.ToString(0,Math.Min(sb.Length,/*String rep limit*/ 1024));
}
/// <summary>
/// Prints the content of the instance into a given string builder.
/// </summary>
protected virtual bool PrintMembers(StringBuilder sb)
{
sb.Append("s = ");
sb.Append((object)s);
sb.Append(",");
sb.Append("ID = ");
sb.Append(ID);
sb.Append(",");
sb.Append("FirstName = ");
sb.Append((object)FirstName);
sb.Append(",");
sb.Append("LastName = ");
sb.Append((object)LastName);
return true;
}
}
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/DebuggerToString
Leave a Reply