Serializing Custom Generic Collection (Of InterfaceType)

  • Thread starter Thread starter Mythran
  • Start date Start date
M

Mythran

We have written a generic class NameObjectCollection(Of T) that inherits
from NameObjectCollectionBase and implements IXmlSerializable. We have an
interface class IBaseCondition and several classes that implement
IBaseCondition (StringCondition, NumericCondition, etc.). We have another
class (SearchCriteria) that has a property (Conditions) which is defined as
Public ReadOnly Property Conditions() As NameObjectCollection(Of
IBaseCondition).

When we use return this SearchCriteria class from a web service, we get an
exception stating "System.NotSupportedException: Cannot serialize interface
IBaseCondition.".

Is there any way I can get this to work? We must be able to serialize the
IBaseCondition since the NameObjectCollection is a collection of
IBaseCondition.

Thanks,

Mythran
 
Mythran said:
We have written a generic class NameObjectCollection(Of T) that inherits
from NameObjectCollectionBase and implements IXmlSerializable. We have an
interface class IBaseCondition and several classes that implement
IBaseCondition (StringCondition, NumericCondition, etc.). We have another
class (SearchCriteria) that has a property (Conditions) which is defined as
Public ReadOnly Property Conditions() As NameObjectCollection(Of
IBaseCondition).

When we use return this SearchCriteria class from a web service, we get an
exception stating "System.NotSupportedException: Cannot serialize interface
IBaseCondition.".

Is there any way I can get this to work? We must be able to serialize the
IBaseCondition since the NameObjectCollection is a collection of
IBaseCondition.

Thanks,

Mythran

Then you need to make IBaseCondition inherit IXmlSerializable.

Mike
 
Family Tree Mike said:
Then you need to make IBaseCondition inherit IXmlSerializable.

Mike

I ended up implementing IXmlSerializable in NameObjectCollection(Of T) and
SearchCriteria. We have numerous classes that implement IBaseCondition and
it would be a headache to add new ones and update the existing ones just for
serialization. So far, it looks like the everything is
serialized/deserialized correctly (I have printed the serialized object as a
string and it looks correct, then took the string and deserialized and the
values of the objects come out correct too).

Thanks,
Mythran
 
Hi Mythran,

Since the XmlSerializer requires a concrete class to instantiate when
deserializing, there is not much options with the current design (a
collection of an interface type).

You may consider based on current implementation, let the classes that
implements the IBaseCondition to do the actual work generating XML during
serialization and instances during deserialization, and in the
NameObjectCollection's IXmlSerializable implementation, call those
individual classes' method to return the XML or instances.

Best regards.

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
"Jie Wang [MSFT]" said:
Hi Mythran,

Since the XmlSerializer requires a concrete class to instantiate when
deserializing, there is not much options with the current design (a
collection of an interface type).

You may consider based on current implementation, let the classes that
implements the IBaseCondition to do the actual work generating XML during
serialization and instances during deserialization, and in the
NameObjectCollection's IXmlSerializable implementation, call those
individual classes' method to return the XML or instances.

Best regards.

Jie Wang

Thanks for your response,

What I ended up doing was implementing IXmlSerializer on the collection
class itself. During serialization, I loop through and serialize each item
in the collection, creating an XmlSerializer for each iteration based on the
type of the object stored in the collection. So far, this works great.

Thanks,
Mythran
 
Back
Top