views:

48

answers:

2

I'm writing a logging framework and need to serialize each object in an IDictionary<string,object>. Object can be a simple string, int etc. or a complex business object or a collection of either. Therefore I need a flexible method for doing this.

At the more complex end of things I'd like the output to be something like:

Object = IList<Person>

  1. List Of Person:
    • Person:
      • Name: David Neale
      • Gender: Male
    • Person:
      • Name: John Smith
      • Gender: Male

I don't need to handle nested to the level of the object containing a collection which contains collections.

Could somebody point me in the right direction?

+1  A: 

Take a look at the answers to this question on SO to get you started.

Basically, there's no built-in mechanism to do so in .NET - however, you can look at some of the objectDump code that's out on the web, like this.

FYI: Apparently the ObjectDumper code is something that is part of the VS C# samples (see Eric White's blog):

You can find Object Dumper in the CSharpSamples.zip file that is installed with Visual Studio 2008.

C:\Program Files\Microsoft Visual Studio 9.0\Samples\1033\CSharpSamples.zip

LBushkin
+1  A: 

Here is an implementation of a serializable dictionary, for reference.

If the values in the dictionary are always serializable, then you are good to go with the serializable dictionary. If they are not, there is no really good solution to do this - if you can't control what is going into the dictionary, you can't guarantee that you are able to serialize it.

If you know the possible types beforehand, but are not able to change them to be serializable, then you could perhaps create formatter objects for each type, and then use a lookup to find the correct formatting object for each value in the dictionary.

driis