Re: [fluent-nhib] Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag to type 'System.Collections.Generic.List

946 views
Skip to first unread message

Rene Felix Correa

unread,
May 20, 2013, 7:34:44 AM5/20/13
to fluent-n...@googlegroups.com

the faster solution is before you serialize this object call the method .ToList();

if your problem is deserialize, make a deserialize with the type list, and set to a IList, like this:


XmlSerializer serializer = new XmlSerializer(typeof(new List<Class5>())); 
 
StreamReader reader = new StreamReader(caminho); 
IList<Class5> listClass = (List<Class5>)serializer.Deserialize(reader); 



On Mon, May 20, 2013 at 12:26 AM, Murali Mohan <moha...@gmail.com> wrote:
I am trying to load the domain class by deserializing an xml file. So I have used 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.PersistentGenericBag`1[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<int> instead of List<int>(http://stackoverflow.com/questions/1638593/unable-to-cast-object-of-type-nhibernate-collection-generic-persistentgenericbag)
 
But, If I use IList then I am unable to Deserialize the xml in to Domain class. It gives another 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.
 
Regards,
Murali

--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fluent-nhibern...@googlegroups.com.
To post to this group, send email to fluent-n...@googlegroups.com.
Visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Rene Felix Correa
Analista Desenvolvedor
http://renefc3.wordpress.com

Murali Mohan

unread,
May 20, 2013, 3:52:08 PM5/20/13
to fluent-n...@googlegroups.com
Actually the Class5 is not used in a list but it has properties of type List. So we are trying to deserialize as an object.
I have also posted the same question HERE with an example. Could you please check it?
Reply all
Reply to author
Forward
0 new messages