S
shapper
Hello,
I followed some examples in internet and in MSDN web site to create a
serializable class.
Basically, I need to serialize a class to binary so I can save it in
an SQL 2005 table using a column of type VARBINARY (SQL 2005). And
also deserialize and return the class.
My problem is how to use the class. Of course I know how to create an
instance of this class.
I also know that I should run the method Serialize so I can get a
binary instance of it and Deserialize, providing a Byte() taked from
the database, to get a class.
I think this far so good.
My problem is when creating the class using NEW one of the
constructors have:
ByVal siRows As SerializationInfo, ByVal scRows As StreamingContext
What should I provide in this?
I created this code based on various examples so I am not sure if I
made some mistakes.
Could someone, please, tell me if I am doing everything right?
Anyway, here is my entire code:
1 <Serializable()> _
2 Public Class Rows
3 Implements ISerializable
4
5 Private _Rows As New Generic.List(Of Row)
6 Public Property Rows() As Generic.List(Of Row)
7 Get
8 Return _Rows
9 End Get
10 Set(ByVal value As Generic.List(Of Row))
11 _Rows = value
12 End Set
13 End Property ' Rows
14
15 Public Sub New()
16 End Sub ' New
17
18 Public Sub New(ByVal siRows As SerializationInfo, ByVal scRows
As StreamingContext)
19 Me.Rows = siRows.GetValue("Rows", GetType(Row))
20 End Sub ' New
21
22 Public Sub GetObjectData(ByVal siRows As SerializationInfo,
ByVal scRows As StreamingContext) Implements
ISerializable.GetObjectData
23 siRows.AddValue("Rows", Me.Rows)
24 End Sub ' GetObjectData
25
26 Public Function Serialize() As Byte()
27 Dim bRows() As Byte = New Byte() {}
28 Dim sRows As Stream = Stream.Null
29
30 Try
31 sRows.Read(bRows, 0, Integer.MaxValue)
32 Dim bfRows As New BinaryFormatter
33 bfRows.Serialize(sRows, Me)
34 Finally
35 sRows.Close()
36 End Try
37 Return bRows
38
39 End Function ' Serialize
40
41 Public Shared Function Deserialize(ByVal data As Byte()) As
Rows
42
43 Dim sRows As Stream = Stream.Null
44 Try
45 sRows.Write(data, 0, data.Length)
46 Dim bfRows As New BinaryFormatter
47 Return CType(bfRows.Deserialize(sRows), Rows)
48 Finally
49 sRows.Close()
50 End Try
51
52 End Function ' Deserialize
53
54 End Class ' Rows
Thanks,
Miguel
I followed some examples in internet and in MSDN web site to create a
serializable class.
Basically, I need to serialize a class to binary so I can save it in
an SQL 2005 table using a column of type VARBINARY (SQL 2005). And
also deserialize and return the class.
My problem is how to use the class. Of course I know how to create an
instance of this class.
I also know that I should run the method Serialize so I can get a
binary instance of it and Deserialize, providing a Byte() taked from
the database, to get a class.
I think this far so good.
My problem is when creating the class using NEW one of the
constructors have:
ByVal siRows As SerializationInfo, ByVal scRows As StreamingContext
What should I provide in this?
I created this code based on various examples so I am not sure if I
made some mistakes.
Could someone, please, tell me if I am doing everything right?
Anyway, here is my entire code:
1 <Serializable()> _
2 Public Class Rows
3 Implements ISerializable
4
5 Private _Rows As New Generic.List(Of Row)
6 Public Property Rows() As Generic.List(Of Row)
7 Get
8 Return _Rows
9 End Get
10 Set(ByVal value As Generic.List(Of Row))
11 _Rows = value
12 End Set
13 End Property ' Rows
14
15 Public Sub New()
16 End Sub ' New
17
18 Public Sub New(ByVal siRows As SerializationInfo, ByVal scRows
As StreamingContext)
19 Me.Rows = siRows.GetValue("Rows", GetType(Row))
20 End Sub ' New
21
22 Public Sub GetObjectData(ByVal siRows As SerializationInfo,
ByVal scRows As StreamingContext) Implements
ISerializable.GetObjectData
23 siRows.AddValue("Rows", Me.Rows)
24 End Sub ' GetObjectData
25
26 Public Function Serialize() As Byte()
27 Dim bRows() As Byte = New Byte() {}
28 Dim sRows As Stream = Stream.Null
29
30 Try
31 sRows.Read(bRows, 0, Integer.MaxValue)
32 Dim bfRows As New BinaryFormatter
33 bfRows.Serialize(sRows, Me)
34 Finally
35 sRows.Close()
36 End Try
37 Return bRows
38
39 End Function ' Serialize
40
41 Public Shared Function Deserialize(ByVal data As Byte()) As
Rows
42
43 Dim sRows As Stream = Stream.Null
44 Try
45 sRows.Write(data, 0, data.Length)
46 Dim bfRows As New BinaryFormatter
47 Return CType(bfRows.Deserialize(sRows), Rows)
48 Finally
49 sRows.Close()
50 End Try
51
52 End Function ' Deserialize
53
54 End Class ' Rows
Thanks,
Miguel