views:

763

answers:

2

I have an interface that describes an specialized list in my application...

Public Interface IAlphabeticListItem

    Property StartingLetter() As String
    Property DetailsList() As Generic.List(Of DetailsData)

End Interface

Multiple objects implement this interface, but I want to return them in a webservice not as the underlying object, but as the interface.

<WebMethod()> _
Public Function GetCategoryList(...) As Generic.List(Of IAlphabeticListItem)

    ...

End Function

Sadly, this causes an exception

Cannot serialize interface IAlphabeticListItem.

Exception Details: System.NotSupportedException: Cannot serialize interface IAlphabeticListItem.

Is there a way to make an interface serializable, or am I going to have to convert each of my objects into a concrete class implementing the interface, and then return that class?

+1  A: 

I don't have time to test it right now, but in C# you can extend interfaces, so I suppose you can do that in VB.NET.

Just make IAlphabeticListItem extend ISerializable

Rafa G. Argente
To do this would mean Implementing GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData in each of my other classes!
digiguru
If the objects implementing that classes are already ISerializable, you shouldn't have any problems. On the other hand, if they're not, I'm afraid you'll need to implement the interface anyways, so this won't make it worse.
Rafa G. Argente
+1  A: 

Yes and no. You cannot directly expose the generic class for XML serialization because it's not supported. You can however expose a non-generic interface on the same collection and serialize that. This blog goes into great detail on how to make this work

http://srtsolutions.com/blogs/billwagner/archive/2006/11/20/xml-serialization-and-generic-interfaces.aspx

JaredPar