473,383 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Serialization

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

Nov 21 '05 #1
2 2589
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
-----------------
"Peter" <zl*****@sina.com> wrote in message
news:Oq*************@TK2MSFTNGP15.phx.gbl...
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


Nov 21 '05 #2
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
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
1
by: andrewcw | last post by:
There is an error in XML document (1, 2). I used XML spy to create the XML and XSD. When I asked to have the XML validated it said it was OK. I used the .net SDK to generate the class. I have...
3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
6
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or...
3
by: Alexander | last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without SP1. An i get this Error: System.Runtime.Serialization.SerializationException: Possible Version mismatch. Type...
4
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
5
by: Nikola Skoric | last post by:
I ran in Mono a program developed on .NET Framework 2.0 and it ran OK until I tried to desirialize a object. There the program died abruptly dumping this: System.ArgumentOutOfRangeException:...
0
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.