Set collection item value through PropertyDescriptor?
If I have a PropertyDescriptor with SerializationVisibility == Content, what is the appropriate way to populate the collection with items?
If SerializationVisibility == Visible, I'd use propertyDescriptor.SetValue() but I don't see how I can add items to a collection.
Related to this question is: What requirements must a property meet in order to mark it with SerializationVisiblity == Content? I suspect it must at least be IEnumerable for the reading the property but I can't figure out what it must provide to write the property.
Thanks,
Ray
I am not sure what scenario you are trying to do this in, but couldn't you do a GetValue() on the PropertyDescriptor and then configure the object as you want?
There are no real requirements for a property to be marked DSV.Content, except that there should an instance available when it is queried and that the object is mutable.
The scenario is in (de)serializing a component. I now understand that there are no real requirements to mark an object with DSV.Content but when I originally posted I thought this attribute was used on collections exclusively. The question better stated would be:
What are the requirements for a collection marked DSV.Content in order to have it's contents serialized/deserialized.
The serializer/deserializer needs to know how to get to and add items to the collection. Does it assume a read/write 'items' property? (no.) Does it assume an Add()/AddRange()? If I implement "class PropertyBag : Object" how does the serializer determine it's a collection and know to serialize the items?
- Ray
Serializing proceeds in a kind of recursive fashion. When a new object is encountered (like the object referenced by the DSV.Content property), we look up the serializer for it and ask it to serialize the object. If the object is of type ICollection, it will get a special serializer called CollectionCodeDomSerializer that knows specifically how to serialize collections (using Add/Add Range etc). If it is not ICollection, then it may get a different serializer, say, the default serializer if no specific one is found.
So what then are the assumptions of the CollectionCodeDomSerializer with respect to ICollections? The documentation states:
The CollectionCodeDomSerializer class can create either statements or expressions. If the object to serialize is an array, the CollectionCodeDomSerializer will create an expression and assign it to the statement in the current context stack. If the object is a collection with an AddRange or similar method, the CollectionCodeDomSerializer will create a statement that calls the method.
What exactly does "similar" mean?
Not all ICollections will be arrays nor will they all have an "AddRange or similar" method. AddRange, by the way, is used by so many 'collections' in .net that it seems like a good candidate for an interface member. ICollection in itself does not provide a means for inserting elelments into a collection and since AddRange is not an interface member, the name itself does not provide an indication that it actually adds elements to the collection. For example, AddRange on an integer collection might actually sum the range and not add the range to the collection. How does the CollectionCodeDomSerializer determine that AddRange is the appropriate method to call?
- Ray