Friday, June 10, 2005

Dotnet gotchas - 2

XML (ha ha ha, it had to come somewhere isn't it) serialization is not symmetric when you have custom collections (.net 1.1). Say u have a class called (psudocode only..)
public class Dummy
{
[XmlElement("Name")]
public string a = "dummy";

[XmlArray("Things")]
public MyCollection col = null;

public Ha(){}
}
When you create an instance of a class and deserialize it you get an xml like
<Dummy>
<Name>dummy</Name>
</Dummy>
Now when you deserialize it you expect to get a equivalent object obj1 with a initialized to "dummy" and col to null. However, what you actually get for col field is an "empty collection" object!! So if you are testing your webservice (this joke had to come somewhere too :) ), a save retrieve sequence will get you an object which is slightly different from what you are expecting (it is a different issue that the webservice can never see null since the transport is xml serialization)!

No comments: