WP7 - A DataContractSerializer Bug ?

Print Content | More

As we saw in the previous article the DataContractSerializer is able to serialize and deserialize class hierarchies, let’s review the example again:

[DataContract]
public class Entity
{
	public int M1;

	[DataMember]
	public string P1 { get; set; }
}

[DataContract]
public class Derived : Entity
{
	[DataMember]
	public string P2 { get; set; }
}
...
// can serialize and deserialize derived classes and class hierarchies
Derived d = new Derived();
d.P1 = "base";
d.P2 = "derived";
string data = DataContractSerializerHelpers.Serialize(d);
MessageBox.Show(data);
Derived deser = DataContractSerializerHelpers.Deserialize(data);
// deser holds the correct values
MessageBox.Show(string.Format("Deserialized values, P1: {0} P2: {1}", deser.P1, deser.P2));

When you run the example you will see the the serialized stream data will look like:


  base
  derived

And the object is also deserialized in a correct way.

But if your derived class DO NOT expose any public or [DataMember] marked member the DataContractSerializer will fail in persisting the object: it will just ignore any of the base class properties and the object will be serialized as empty, let’s see some sample code:

[DataContract]
public class Entity
{
	public int M1;

	[DataMember]
	public string P1 { get; set; }
}

[DataContract]
public class DerivedBug : Entity
{

}
...
// if the derived class has no public property or datamember the object is not serialized correctly:
// none of the base class properties are persisted
DerivedBug d = new DerivedBug();
d.P1 = "base";
string data = DataContractSerializerHelpers.Serialize(d);
MessageBox.Show(data);
DerivedBug deser = DataContractSerializerHelpers.Deserialize(data);
// the object is deserialized but all its properties have the default values (that is P1 is null)
MessageBox.Show(string.Format("Deserialized values, P1: {0} (it should be 'base')", string.IsNullOrEmpty(deser.P1) ? "null" : deser.P1));

When you serialize the ‘DerivedBug’ class, you will obtain a data stream like the following:

This means that the object will exists, but its deserialized properties will have their default values (also you have to remember that your parameterless constructor will not be called too). So you will get back an empty instance of the DerivedBug class.

To workaround this issue you will have to add a ‘fake’ property to your class and just avoid using it (add proper documentation to explain why that property is there):

/// 
/// This is a workaround to a bug in WP7 DataContractSerializer
/// where no data will be serialized for a class if it
/// doesn't have an explicit DataMember property or field.
/// 
[DataMember]
public int U;

For a complete series of examples you can download the attached project:



WP7, Serialization, DataContract, Bug

0 comments

All fields are required and you must provide valid data in order to be able to comment on this post.


(will not be published)
(es: http://www.mysite.com)


  1. #1 da http://www.alvinashcraft.com/2011/02/10/dew-drop-february-10-2011/

    Dew Drop – February 10, 2011 | Alvin Ashcraft's Morning Dew