Oftentimes, I need to serialize an object, either for logging or debugging. This is a one-way serialization -- I don't need to get it back out later, I just need to turn an object into a string to write it somewhere.
Yes, yes -- this is why you should always override the ToString
method. I know this. But I'm often dealing with objects I didn't write and can't change. Additionally, I don't want to have to write and update a ToString method for every class I write.
XML serialization offers a seemingly perfect solution -- just flatten that object out into XML. But there are so many limitations, specifically that you can't serialize IDictionary, and you have to have a parameterless constructor. I can get around these in my classes, but -- again -- I'm often working with other people's classes.
So, what's the solution to getting an comprehensive string representation of an object? Is there something simple that I'm missing?