Silverlight: DataContractSerializer cannot handle read only collection properties

Posted by moonground.de on Stack Overflow See other posts from Stack Overflow or by moonground.de
Published on 2010-05-07T08:11:50Z Indexed on 2010/05/07 8:18 UTC
Read the original article Hit count: 775

Hey Stackoverflowers :)

For our Silverlight Project (SL4) I'm using a Model which might contain Lists (IList<AnotherModel>). According to good practice and rule CA2227:CollectionPropertiesShouldBeReadOnly the IList properties don't have a public setter. We serialize the Model using the DataContractSerializer which is working. But when I try to deserialize, a SecurityException is thrown by DataContractSerializer's ReadObject(Stream) Method, complaining that the target property (pointing to the IList property) cannot be set due to a missing public setter.

Since the DataContractSerializer is sealed and neither extendable nor flexible so I currently see no chance to add some kind of additional rules which allow to deserialize the ILists using a foreach-loop on Add() method or some other method of transferring the collection items.

I've also tried to dig into DataContractSerializer source (using Reflector) to create a little fork but it looks like i'd have to dig very deep and replicating whole serialization classes doesn't seem to be a viable solution.

Do you see another chance to serialize a List with no public setter using the DataContractSerializer?

Thank you very much in advance for your ideas!

Thomas

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about silverlight-4.0