By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,681 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,681 IT Pros & Developers. It's quick & easy.

Does Serialization Work On Structures?

P: n/a
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?
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.BinaryForm atter
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
"Daryll SHatz" <ds****@cox.net> wrote in message
news:Og**************@TK2MSFTNGP12.phx.gbl...
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?

Nov 20 '05 #2

P: n/a
Daryll,
As Justin stated, you need to mark the Structure as Serializable as he
demonstrated.

Also the following articles covers every thing you wanted to know about
serialization but were afraid to ask:

http://msdn.microsoft.com/msdnmag/issues/02/04/net/
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

Hope this helps
Jay

"Daryll SHatz" <ds****@cox.net> wrote in message
news:Og**************@TK2MSFTNGP12.phx.gbl...
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?

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.