I have record with many members and I want Console.WriteLine(myRecord); to output its values. However one of its members is an array and it will be printed as MyOtherRecordType.Item[]
Is there a an easy way to customize output of just that single member without rewriting ToString() completeley?
Here is a very simple repro (fiddle if you want):
using System;
public class Program
{
public static void Main()
{
Console.WriteLine(new Test("a", "b", "c"));
}
}
record Test(params string[] strings);
The output is:
Test { strings = System.String[] }
And I want it to be more like
Test { strings = {"a", "b", "c"} }
or different, the point is to see values. In my case it's actually not a string[], but another record type array.
I wish there was an attribute, like with json converters, to force ToString() to use converter, but I am afraid the answer is not possible and I have to override ToString() completely. Maybe I can somehow combine the autogenerated ToString() with my code?
I am looking at msdn, but there is nothing inspirational.
Not really a solution, more of a work-around would be to serialize to JSON:
In Action: https://dotnetfiddle.net/y3LQaB
Output:
{"strings":["a","b","c"]}Or a little closer to the desired output:
https://dotnetfiddle.net/pzJHhn
=>
Test{"strings":["a","b","c"]}