Does Serialization Work On Structures?

D

Daryll SHatz

Does serialization work on custom data types via structures?

Dim bFormatter As New BinaryFormatter()
Dim sFile As FileStream = File.Create(FilePath)
Dim structure As myStruct
Dim val1 as Integer
Dim Val2 as String
End Structure
Dim aStruct as myStruct

aStruct.val1 = 123
aStruct.val2 = "some text"

bFormatter.Serialize(sFile, aStruct)
sFile.Close()

Everytime I use serialization on structures I get a PublicKeyToken=null is
not marked as serializable error. I can find much on serialization. Does
the object need some special interface to be serializ-able?
 
J

Justin Weinberg

Yes it does work. Make sure you marked your structure with the
<Serializable()> attribute. This tells the framework your object/structure
is ok to serialize:


<Serializable()> _
Structure foo
Dim val1 As Integer
Dim Val2 As String
End Structure

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim bFormatter As New
Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim sFile As System.IO.FileStream = System.IO.File.Create("c:\test")
Dim x As foo
x.val1 = 123
x.Val2 = "some text"
bFormatter.Serialize(sFile, x)
sFile.Close()
End Sub



--
Justin Weinberg

Designing a PrintDocument or creating .NET graphics?
Save time with GDI+ Architect.
For more information, visit http://www.mrgsoft.com
 

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