【问题标题】:is it possible to return an array of object Interface's rather than an object?是否可以返回对象接口的数组而不是对象?
【发布时间】:2009-03-04 12:31:47
【问题描述】:

我的应用程序中有一个描述专用列表的界面...

Public Interface IAlphabeticListItem

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

End Interface

多个对象实现此接口,但我想在 Web 服务中返回它们,而不是作为底层对象,而是作为接口。

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

    ...

End Function

很遗憾,这会导致异常

无法序列化接口 IAlphabeticListItem。

异常详细信息:System.NotSupportedException:无法序列化接口 IAlphabeticListItem。

有没有办法使接口可序列化,还是我必须将我的每个对象转换为实现接口的具体类,然后返回该类?

【问题讨论】:

    标签: vb.net web-services interface asmx


    【解决方案1】:

    是和不是。您不能直接公开用于 XML 序列化的泛型类,因为它不受支持。但是,您可以在同一个集合上公开一个非通用接口并对其进行序列化。这篇博客详细介绍了如何完成这项工作

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

    【讨论】:

      【解决方案2】:

      我现在没有时间测试它,但是在 C# 中你可以扩展接口,所以我想你可以在 VB.NET 中做到这一点。

      只需让 IAlphabeticListItem 扩展 ISerializable

      【讨论】:

      • 这样做意味着实现 GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) 在每个中实现 System.Runtime.Serialization.ISerializable.GetObjectData我的其他课程!
      • 如果实现该类的对象已经是 ISerializable,那么您应该没有任何问题。另一方面,如果不是,恐怕您无论如何都需要实现接口,所以这不会使情况变得更糟。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-11
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多