I am trying to load the domain class by deserializing an xml file. So I have used System.Collections.Generic.List in the domain class. But when I try to save the object using Session object then it is failing with exception "Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag1[MyFirstMapTest.Class5]' to type 'System.Collections.Generic.List
1[MyFirstMapTest.Class5]'." This issue was posted in some of the previous discussion and the answer was to use use IList instead of List(Unable to cast object of type NHibernate.Collection.Generic.PersistentGenericBag to List)
But, If I use IList then I am unable to Deserialize the xml in to Domain class.
XmlTextReader xtr = new XmlTextReader(@"C:\Temp\SampleInput.xml");
XmlSerializer serializer = new XmlSerializer(objClass5.GetType());
objClass5 = (MyFirstMapTest.Class5)serializer.Deserialize(xtr);
session.Save(objClass5);
It is throwing the below error "Cannot serialize member xxxxx of type System.Collections.Generic.IList`1[[xxxxxxxxxx, Examples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] because it is an interface."
I have tried to use PersistentGenericBag instead of List but PersistentGenericBag is not serializable. So Deserialization is not working.
How can I resolve this issue? Thank you for looking at this issue.
You can create two properties like this in your class:
public class Sample
{
private IList<Sample> _list;
[XmlIgnoreAttribute]
public virtual IList<Sample> List
{
get
{
return _list;
}
set
{
_list = value;
}
}
public virtual List<Sample> List
{
get
{
return (List<Sample>)_list;
}
set
{
_list = value;
}
}
}
And you only map your IList Property.