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 2 2591
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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. ...
|
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...
|
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...
|
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...
|
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:...
|
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...
|
by: kikisan |
last post by:
I am developing a windows service which utilizes the following classes:
interface IPersistable;
abstract class PersistableObject : IPersistable;
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |