开发者

Implementation of a general-purpose object structure (property bag)

开发者 https://www.devze.com 2022-12-18 10:33 出处:网络
We need to implement so开发者_如何学运维me 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. T

We need to implement so开发者_如何学运维me 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).


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.


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");


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消