什么类可序列化多维数组? [英] What Class for Serializable Multidimensional Arrays?

查看:138
本文介绍了什么类可序列化多维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:请参阅下文



我有一个Web服务使用一类函数返回在各种业务流程中使用的数据(通过InfoPath)。



其中一个函数使用给定的SQLCommand对象,并将其执行到SQLDataReader中。现在,根据所使用的SQL命令文本,这可能返回一行或多行的一行或多行。那么什么是这个函数的最好的类返回方面需要记住它需要由web服务序列化。



我现有的代码是:

  Dim array As New ArrayList 

Try

conCMS.Open()
Dim rdr As SqlDataReader = cmdCurrent.ExecuteReader

While rdr.Read
如果rdr.VisibleFieldCount> 1 Then
Dim complexType(rdr.VisibleFieldCount - 1)As String
对于rowIndex As Integer = 0到rdr.VisibleFieldCount - 1
complexType(rowIndex)= rdr(rowIndex)
下一个
array.Add(complexType)
Else
array.Add(rdr(0))
结束如果
结束而
conCMS.Close

返回数组

Catch ex As Exception

array.Add(ERROR& ex.Message)
$ b b结束尝试

返回没有

现在我知道这不是有效的代码,但这是一个正在进行的工作。



您可能会看到,生成一个字符串数组来表示具有多个列的行,但是这不能由Web服务序列化。



所以真的有两件事:




  • 有关使用有效类型的指导



感谢您的帮助。
我已经设法通过简单地创建一个嵌套的序列化来实现序列化工作。 arrayList如下(doh!):

 如果rdr.VisibleFieldCount> 1 then 
Dim complexType As New ArrayList
对于rowIndex As Integer = 0到rdr.VisibleFieldCount - 1
complexType.Add(rdr(rowIndex))
接下来
数组.Add(complexType)
Else
array.Add(rdr(0))
如果
结束

但是,请让我知道如何改进。

解决方案

的调用之前返回的数据,那么你应该返回相同形状的数据。对返回数据的每一列都有一个具有相应类型的属性的结构或类。对于每一行,创建此类结构的实例,并从返回的列填充属性。然后将每个实例添加到该结构的强类型列表中,即List(Of T)。然后返回列表。



OBTW,ArrayList是在.NET中使用泛型之前创建的。今天,最好使用强类型的集合,而不是ArrayList,它基本上是List(Of anything )。


EDIT: See Below

I have a web service which uses a class of functions in order to return data used in various business processes (via InfoPath).

One of the functions takes a given SQLCommand object and executes it into a SQLDataReader. Now depending on the SQL command text used this may return one or many rows of one or many columns. So what is the best class for this function to return bearing in mind it needs to be serialized by the web service.

My existing code is:

    Dim array As New ArrayList

    Try

        conCMS.Open()
        Dim rdr As SqlDataReader = cmdCurrent.ExecuteReader

        While rdr.Read
            If rdr.VisibleFieldCount > 1 Then
                Dim complexType(rdr.VisibleFieldCount - 1) As String
                For rowIndex As Integer = 0 To rdr.VisibleFieldCount - 1
                    complexType(rowIndex) = rdr(rowIndex)
                Next
                array.Add(complexType)
            Else
                array.Add(rdr(0))
            End If
        End While
        conCMS.Close()

        Return array

    Catch ex As Exception

        array.Add("ERROR " & ex.Message)

    End Try

    Return Nothing

Now I know this is not efficient code, but this a work in progress.

As you can probably see this is generating a string array to represent a row with more than one column, however this cannot be serialized by the web service.

So 2 things really;

  • Some guidance on an effective type to use (without writing a serializable class of my own)
  • Some advice on improving the code going forward.

Thanks in advance

EDIT: I have managed to get serialization to work by simply creating a nested arrayList as follows (doh!):

   If rdr.VisibleFieldCount > 1 Then
    Dim complexType As New ArrayList
    For rowIndex As Integer = 0 To rdr.VisibleFieldCount - 1
      complexType.Add(rdr(rowIndex))
    Next
    array.Add(complexType)
   Else
    array.Add(rdr(0))
   End If

However please let me know how this could be improved.

解决方案

If your code knows the schema of the returned data before the call, then you should return data in the same shape. Have a struct or class with properties of the appropriate type for each column of the returned data. For each row, create an instance of such a struct, and fill in the properties from the returned columns. Then add each instance to a strongly-typed list of that struct, a List(Of T). Then return the list.

OBTW, ArrayList was created before we had generics in .NET. Today, it's better to use strongly-typed collections and not ArrayList, which is basically List(Of anything).

这篇关于什么类可序列化多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆