所以我创建了一个名为Report的对象。 Report对象有几个不同的属性,包括DataSet。此DataSet应包含至少一个DataTable。报告是可序列化的。在单元测试中,我实例化一个Report对象,给它的属性值,然后序列化它,然后反序列化它。
添加一个方法来填充DataSet后,Deserialization函数给出了Error:无法转换类型&System; System.Data.DataTable'输入' System.Collections.IEnumerable'。 不应该将其序列化为DataSet ...而不是DataTable吗?我不确定是什么原因导致我这个错误。代码片段如下。任何帮助表示赞赏!
<Serializable> Public Class Report
Private titleStr As String
Private startDateTime As DateTime
Private endDateTime As DateTime
Private numIntervalsShort As Short
Private binnedDataSet As DataSet
...property gets and sets...namely:
Property BinnedData As DataSet
Get
Return binnedDataSet
End Get
Set(ByVal value As DataSet)
binnedDataSet = value
End Set
End Property
End Class
<TestClass()> Public Class ReportObjectTest
<TestMethod()> Public Sub TestCreateReport()
Dim testReport As New Project.Report
testReport.Title = "Test Title"
testReport.StartDate = "1/1/2015 1:00 AM"
testReport.EndDate = "1/2/2015 6:00 PM"
testReport.NumIntervals = 41
PopulateDataSet(testReport)
Serialize(testReport)
Deserialize()
End Sub
Private Sub PopulateDataSet(ByRef report As Project.Report)
report.BinnedData = New DataSet()
Dim DT1 As DataTable = New DataTable("Test Table1")
report.BinnedData.Tables.Add(DT1)
DT1.Columns.Add(New DataColumn("Column1", GetType(Int32)))
DT1.Columns.Add(New DataColumn("Column2", GetType(Int32)))
DT1.Columns.Add(New DataColumn("Column3", GetType(Int32)))
Dim Row1 As DataRow = DT1.NewRow()
Dim Row2 As DataRow = DT1.NewRow()
Dim Row3 As DataRow = DT1.NewRow()
Row1("Column1") = 32
Row1("Column2") = 15
Row1("Column3") = 9
Row2("Column1") = 3
Row2("Column2") = 27
Row2("Column3") = 98
Row2("Column1") = 1
Row2("Column2") = 12
Row2("Column3") = 65
End Sub
Private Sub Serialize(ByRef report As Project.Report)
Dim stream As New FileStream("TestReport.xxx", FileMode.Create)
Dim mySerializer As BinaryFormatter = New BinaryFormatter()
Try
mySerializer.Serialize(stream, report)
Catch ex As SerializationException
Console.WriteLine("Error saving the report. " & ex.Message)
Throw
Finally
stream.Close()
End Try
End Sub
Private Sub Deserialize()
Dim readTestReport As New Project.Report()
Dim stream As New FileStream("TestReport.xxx", FileMode.Open)
Dim myDeserializer As BinaryFormatter = New BinaryFormatter()
Try
readTestReport = DirectCast(myDeserializer.Deserialize(stream), Project.Report)
Catch ex As SerializationException
Console.WriteLine("Error opening the report. " & ex.Message)
Throw
Finally
stream.Close()
End Try
Console.WriteLine(Environment.NewLine & "Values after Deserialization:")
Console.WriteLine("Title = " & readTestReport.Title)
Console.WriteLine("Start Date = " & readTestReport.StartDate)
Console.WriteLine("End Date = " & readTestReport.EndDate)
Console.WriteLine("Number of Intervals = " & readTestReport.NumIntervals)
For Each Table In readTestReport.BinnedData.Tables
For Each Row In Table
Console.WriteLine("Table Row = " & Row.ToString)
For Each DataColumn In Row
Console.WriteLine(DataColumn.ToString)
Next
Next
Next
End Sub
End Class
答案 0 :(得分:0)
我是对的......明白了。问题在于我在反序列化后如何尝试显示表的值。 DataTables和DataRows不是可枚举的,但我试图显示它的方式是循环使用只能使用可枚举类型的方式。
我修改了该块以将表分配给View:
Dim tableView As DataView = New DataView(readTestReport.BinnedData.Tables(0))
Console.WriteLine("Table = ")
Dim rowView As DataRowView
Dim i As Integer
For Each rowView In tableView
For i = 0 To tableView.Table.Columns.Count - 1
Console.Write(rowView(i) & vbTab)
Next
Console.WriteLine()
Next
这些文章有助于:
https://msdn.microsoft.com/en-us/library/23a0aths%28v=vs.110%29.aspx
https://msdn.microsoft.com/en-us/library/bb669099%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1