Serialization

P

Peter

Hi,

If I Serialized a instance of class zCMethod and then deserialized it, no
error occurs.
Now ,I Serialized a instance of class InheritedCMethod and then deserialized
it, I got an error.
system: MissingMemberException: FieldInfo can't match destination type¡£

I know the error appears only because I have defined a array Geometry(). But
I do need the array,how to deal with it?

Thanks in advance,

Peter

Below is my code:

Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.binary

Imports System.Data.OleDb

<Serializable()> Public Class zCMethod
public Name as string
Public Specimen As Specimens

Protected mSubType As Short


Sub New()

Specimen.Initialize()

End Sub

<Serializable()> Structure Specimens
Dim Geometry() As Single

Public Sub Initialize()
ReDim Geometry(9)
End Sub

End Structure

End Class


<Serializable()> Public Class InheritedCMethod
Inherits zCMethod

Sub New( ByVal iSubType As Short)
MyBase.new()
msubtype = iSubType

End Sub

Public Sub SerializeMethod()
If File.Exists("MyMethod.bin") Then File.Delete("MyMethod.bin")

Dim Formatter As New BinaryFormatter
Dim Stream As New FileStream("MyMethod.bin", FileMode.Create, _
FileAccess.Write, FileShare.None)
Formatter.Serialize(Stream, Me)
Stream.Close()

End Sub

Public Sub DeserializeMethod()
If System.IO.File.Exists("MyMethod.bin") Then
Dim Formatter As New BinaryFormatter
Dim streamRead As New FileStream("MyMethod.bin",
FileMode.Open, _
FileAccess.Read,
FileShare.Read)

Formatter.Deserialize(streamRead)
streamRead.Close()

End If

End Sub

End Class


sub Main
Dim zMethod As InheritedCMethod = New InheritedCMethod(0)

zMethod.SerializeMethod()

zMethod.DeserializeMethod()

end sub
 
K

Ken Tucker [MVP]

Hi,

The binary formatter deserialize method returns the data. I
converted the code in the 101 vb.net samples How-To Serializing Objects to
work with an array. Here are the changes to the binary serialization. Hope
this helps.

Private Sub cmdStandardSerializationBinary_Click(ByVal sender As
System.Object, _
ByVal e As System.EventArgs) _
Handles cmdStandardSerializationBinary.Click
'This routine creates a new instance of Class1, then serializes it to
'the file Class1File.dat using the Binary formatter.

'Create the object to be serialized
Dim c(500) As Class1

For x As Integer = 0 To 500
c(x) = New Class1(CInt(Rnd() * 200), CInt(Rnd() * 100),
CInt(Rnd() * 200))
Next

'Get a filestream that writes to strFilename2
Dim fs As New FileStream(strFileName2, FileMode.OpenOrCreate)

'Get a Binary Formatter instance
Dim bf As New BinaryFormatter

'Serialize c to strFileName2
bf.Serialize(fs, c)

'Close the file and release resources (avoids GC delays)
fs.Close()

'Deserialization is now available
cmdStandardDeserializationBinary.Enabled = True

End Sub

Private Sub cmdStandardDeserializationBinary_Click(ByVal sender As
System.Object, _
ByVal e As System.EventArgs) _
Handles cmdStandardDeserializationBinary.Click
'This routine deserializes an object from the file Class1File.dat
'and assigns it to a Class1 reference.

'Declare the reference that will point to the object to be deserialized
Dim c() As Class1

'Get a filestream that reads from strFilename2
Dim fs As New FileStream(strFileName2, FileMode.Open)

'Get a Binary Formatter instance
Dim bf As New BinaryFormatter()

'Deserialize c from strFilename2
'Note that the deserialized object must be cast to the proper type.
c = CType(bf.Deserialize(fs), Class1())

'Close the file and release resources (avoids GC delays)
fs.Close()

'Put the deserialized values for the fields into the textboxes
txtXAfter.Text = CStr(c(0).x)
txtYAfter.Text = CStr(c(0).GetY)
txtZAfter.Text = CStr(c(0).z)

For x As Integer = 0 To c.GetUpperBound(0)
Trace.WriteLine(String.Format("{0} = {1}, {2}, {3}", x, c(x).x,
c(x).GetY, c(x).z))
Next
'Reset button after deserializing
cmdStandardDeserializationBinary.Enabled = False

End Sub

Ken
-----------------
Hi,

If I Serialized a instance of class zCMethod and then deserialized it, no
error occurs.
Now ,I Serialized a instance of class InheritedCMethod and then deserialized
it, I got an error.
system: MissingMemberException: FieldInfo can't match destination type¡£

I know the error appears only because I have defined a array Geometry(). But
I do need the array,how to deal with it?

Thanks in advance,

Peter

Below is my code:

Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.binary

Imports System.Data.OleDb

<Serializable()> Public Class zCMethod
public Name as string
Public Specimen As Specimens

Protected mSubType As Short


Sub New()

Specimen.Initialize()

End Sub

<Serializable()> Structure Specimens
Dim Geometry() As Single

Public Sub Initialize()
ReDim Geometry(9)
End Sub

End Structure

End Class


<Serializable()> Public Class InheritedCMethod
Inherits zCMethod

Sub New( ByVal iSubType As Short)
MyBase.new()
msubtype = iSubType

End Sub

Public Sub SerializeMethod()
If File.Exists("MyMethod.bin") Then File.Delete("MyMethod.bin")

Dim Formatter As New BinaryFormatter
Dim Stream As New FileStream("MyMethod.bin", FileMode.Create, _
FileAccess.Write, FileShare.None)
Formatter.Serialize(Stream, Me)
Stream.Close()

End Sub

Public Sub DeserializeMethod()
If System.IO.File.Exists("MyMethod.bin") Then
Dim Formatter As New BinaryFormatter
Dim streamRead As New FileStream("MyMethod.bin",
FileMode.Open, _
FileAccess.Read,
FileShare.Read)

Formatter.Deserialize(streamRead)
streamRead.Close()

End If

End Sub

End Class


sub Main
Dim zMethod As InheritedCMethod = New InheritedCMethod(0)

zMethod.SerializeMethod()

zMethod.DeserializeMethod()

end sub
 
P

Peter

Hi, Ken,

Thanks for your reply. But the array I defined is inside a structure. It's
not an instance array of a class.

I have modified and tested my codes many times and drawn my conclusion :

Only in one case I can get the error I wrote before, that is:

1. Define a structure which must contain at least one unfixed array
2. Define a class which contains a varible whose data type is the structure
3. Define the second class which inherits from the class on line 1.
4. Serialize an instance of the second class,no error occurs.
5. Deserialize the file from line 4. and you can get the error I mentioned.

Also, if I serialize and then deserialize an instance of the first class no
error appears.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top