Home All Groups Group Topic Archive Search About

Serializing Custom Generic Collection (Of InterfaceType)

Author
1 Jul 2009 4:42 PM
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

Author
1 Jul 2009 5:31 PM
Family Tree Mike
Show quote Hide quote
"Mythran" wrote:

> 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
Are all your drivers up to date? click for free checkup

Author
1 Jul 2009 11:11 PM
Mythran
Show quote Hide quote
"Family Tree Mike" <FamilyTreeM***@discussions.microsoft.com> wrote in
message news:6EB9D9E8-66CE-4F11-BC37-592BCADE9069@microsoft.com...
>
>
> "Mythran" wrote:
>
>> 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

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
Author
2 Jul 2009 12:05 PM
Jie Wang [MSFT]
Hi Mythran,

>> 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.

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:
msd***@microsoft.com.

==================================================
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.
Author
2 Jul 2009 3:25 PM
Mythran
""Jie Wang [MSFT]"" <jie***@online.microsoft.com> wrote in message
Show quoteHide quote
news:BTQ0o1w#JHA.2552@TK2MSFTNGHUB02.phx.gbl...
> Hi Mythran,
>
>>> 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.
>
> 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

Bookmark and Share