This tutorial shows how to deserialize object
If you not read following topics , please read them first then read this article
Initializing Serializer (Simple)How to Serialize an Object (Simple)Now it is time to deserialize
in previous section we have serialized object in Main.txt now we deserialize object from File
You Must use same compiler Info for deserialization
You must create Text Reader and State Reader
Dim txt = File.ReadAllText("Main.txt")
Using sr As New StringReader(txt), sd As New StateReader(s)
Dim obj = sd.ReadState(sr)
End Using
The full source is following
In a console Application
Imports StateScript
Imports System.IO
Public Class ToBeTester
Public Field1 = "String1"
Public Field2 = "String2"
Public Property Prop1 = 999
Public Num = Me
End Class
Public Module Main
Sub Main()
Dim s As New CompilerInfo
'Add Providers which serialize specified types
s.AddProviders({New BooleanTypeProvider,
New ValueTypesProvider,
New StringTypeProvider,
New CollectionTypesProvider,
New ObjectTypeProvider})
s.Collection = "," 'The Char to separate collection items from each other
s.EndBlock = ")" 'The Char to identify the end of a block
s.StartBlock = "(" 'The Char to Identify the start of a block
s.VariableName = "$" 'The char after which variable(field) Name is written
s.VariableValue = ">" 'The Char after which value of variable(field) is written
Using sw As New StringWriter, sx As New StateWriter(s)
sx.WriteState(New ToBeTester, sw)
File.WriteAllText("Main.txt", sw.ToString)
Dim txt = File.ReadAllText("Main.txt")
Using sr As New StringReader(txt), sd As New StateReader(s)
Dim obj = sd.ReadState(sr)
End Using
End Using
End Sub
End Module