Thursday, April 28, 2011

Determining which properties to serialize in a class that's passed over a webservice

I'm using NHibernate to administer my entities, and to have lazy loading enabled I need to make my properties return an IList<>. Problem is that .NET throws an exception as it can't serialize an interface when I'm trying to pass the entity. This makes perfect sense.

What I need to know is how I can control which fields to serialize, and which not to? My best bet so far is to work around this problem by copying the contents of IList<> into a List<> before serializing the object, but to do that I need to tell .NET that I don't want the IList<> property serialized :)

From stackoverflow
  • MSDN has an area on Serializing Objects, but what you want is Selective Serialization. So basically, you can mark any property you don't want serialized with the attribute, [NonSerialized]. There is an example in the second link.

    cwap : Thanks a lot .. You helped me a lot with those resources, however the correct attribute was System.Xml.Serialization.XmlIgnore , which I'll post in a new answer for further reference.
    Mr. Will : Your welcome. I should have asked if you were doing binary or XML serialization. It has been a long time since I did serialization in .NET.
    cwap : I might be wrong here, but since I'm using a webservice, ain't it supposed to be soap, which in turn means that it needs to be XML-serialization? :P
    John Saunders : Yes, you need XML serialization. This is not the answer to your question.
  • Just wanted to let you guys know that I found the answer to be the [System.Xml.Serialization.XmlIgnore] attribute :)

0 comments:

Post a Comment