Hi,
we're using NH + NHContrib 0.5. We have a business class with a NullableInt32 property. We successfully manage to load it from the database.
Later though, we serialize the objects using XmlSerializer (to allow reloading them in "offline" mode). It seems that the serialization (the way we use it at least!) doesn't handle NullableInt32 properly.
Here's a small repro:
Code:
using System;
using System.Collections;
using NUnit.Framework;
using System.IO;
using System.Xml.Serialization;
using Nullables;
namespace NullableInt32Tests
{
[TestFixture]
public class NullableInt32Serialize
{
[Test,Explicit]
public void SerializeUnserialize()
{
IList list=new ArrayList();
list.Add(new Foo(new NullableInt32(10)));
Assert.AreEqual(10,((Foo)list[0]).MyInt.Value);
XmlSerializer serializer=new XmlSerializer(typeof(ArrayList),new Type[] { typeof(Foo) });
FileStream stream=new FileStream("NullablesInt32Test.xml",FileMode.Create);
serializer.Serialize(stream,list);
stream.Close();
serializer=new XmlSerializer(typeof(ArrayList),new Type[] { typeof(Foo) });
stream=new FileStream("NullablesInt32Test.xml",FileMode.Open);
list= (ArrayList)serializer.Deserialize(stream);
stream.Close();
Assert.AreEqual(1,list.Count);
Foo foo=list[0] as Foo;
// this assertion will fail
Assert.IsTrue(foo.MyInt.HasValue);
Assert.AreEqual(10,foo.MyInt.Value);
}
}
[Serializable]
public class Foo
{
public Foo()
{
}
public Foo(NullableInt32 myInt)
{
_myInt=myInt;
}
private NullableInt32 _myInt;
public NullableInt32 MyInt
{
get { return _myInt; }
set { _myInt=value; }
}
}
}
Does anyone see some obvious error ? Or is there something to be changed in NullableInt32 ?
best regards
Ludovic