var sample = new
{
Time = DateTime.Now,
Name = "Hello"
};
Trace.TraceInformation("{0}", sample);
outputs as
ProcessInvocation86.exe Information: 0 : { Time = 04.11.2012 22:07:52,
Name = Hello }
I’d like different formatting in my application. Is there a way to change the implementation of ToString() for anonymous objects in C#? Maybe some static field per AppDomain or something?
No, you can’t do this –
ToString,Equals, andGetHashCodehave default implementation provided by framework. To override this functionality you should inherit from your anonymous type, which is impossible.Use
String.Formatto get desired output.