tags:

views:

211

answers:

3

We need to implement some general-purpose object structure, much like an object in dynamic languages, that would give us a possibility of creating the whole object graph on-the-fly. This class has to be serializable and somehow user friendly.

So far we have made some experiments with class derived from Dictionary<string, object> using the dot notation path to store properties and collections in the object tree. We have also find an article that implements something similar, but it doesn't seem to fit completely into our picture either.

Do you know about some good implementations / libraries that deal with a similar problem or do you have any (non-trivial) ideas that could help us with our own implementation ?

Also, I probably have to say that we are using .NET 3.5, so we can't take advantage of the new features in .NET 4.0 like dynamic type etc. (as far as I know it's also not possible to use any subset of it in .NET 3.5 solution).

+2  A: 

I've done property-bag implementations in the past, including all the muck like ICustomTypeDescriptor / ITypedList to get it binding - and it can be a lot of work. Especially if you include serialization (not including BinaryFormatter, which has its own issues).

This type of dynamic object doesn't really fit very well in a generally statically typed language like C#, but it can be made to work. While I'm not their greatest fan (not even a little), could you perhaps just use DataTable / DataRow here? It does what you ask, without the many man-hours development / debugging.

Marc Gravell
+2  A: 

One class that's easy to dynamically add properties to (in a hierarchical manner) and serialize is XDocument.

You're not going to get much easier to use unless you move to .Net 4.

I particularly like the way they've added explicit cast operators:

XElement address = d.Element("Address");
int number = (int)address.Attribute("Number");
Rob Fonseca-Ensor
A: 

Have a look at this CodeProject article that explains how to set up a property bag to be used with a property grid here, and also here. I used these as there was an emphasis on your question in relation to a property bag.

Hope this helps, Best regards, Tom.

tommieb75
The second one is the same that I have posted. But I will check out the first one, thanks ;)
Thomas Wanner