This is a mirror of official site: http://jasper-net.blogspot.com/

DataMember Doesn't Work With CollectionDataContract

| Tuesday, June 14, 2011
Confession time. One of my apps has a bug. When it tombstones, the app state is lost. So I was working away on getting my class to serialize into IsolatedStorage properly.

One of my classes was an extension to a Collection. In a somewhat lazy fashion I inherited from a System.Collection.Generic.List and added a few methods and properties inside the class. To make the collection serializable I added the CollectionDataContract attribute to the class. I also attributed the 'extra' members with DataMember attributes in order to get them to serialize as well. Parts of the class contained below.

namespace DevFish
{
    [CollectionDataContract(ItemName = "Bucket", Namespace = "http://www.devfish.net")]
    public class Bucket : System.Collections.Generic.List<int>
    {
        [[DataMember()]
        public int MinElement { get; set; }  
  
        [[DataMember()]
        public int MaxElement { get; set; }

        [[DataMember()]
        public bool DoFullCheck = true;
        .... remainder of class removed for sake of brevity
    }   

So happily I got running my programs and get very unhappy results. Upon inspecting the file content in isolated storage I note while the List content is there, the DataMember attrib'd properties didn't show up in the serialization. File contents below...

<Bucket><Bucket>2</Bucket><Bucket>4</Bucket><Bucket>5</Bucket></Bucket>
<MaxValue>9</MaxValue><MinValue>1</MinValue></Randomizer>

Turns out, properties attrib'd DataMember inside a CollectionDataContract ( unlike regular DataContract ) do not serialize. So what to do? My approach. Wrap the collection class in a non-collection class, then attrib the collection as a DataMember . The main headache was surfacing out some methods like IndexOf and Add that I was using.

Posted via email from Jasper-net

0 comments: