本文整理匯總了VB.NET中System.SerializableAttribute類的典型用法代碼示例。如果您正苦於以下問題:VB.NET SerializableAttribute類的具體用法?VB.NET SerializableAttribute怎麽用?VB.NET SerializableAttribute使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了SerializableAttribute類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Main
' 導入命名空間
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Soap
Public Class Test
Public Shared Sub Main()
' Creates a new TestSimpleObject object.
Dim obj As New TestSimpleObject()
Console.WriteLine("Before serialization the object contains: ")
obj.Print()
' Opens a file and serializes the object into it in binary format.
Dim stream As Stream = File.Open("data.xml", FileMode.Create)
Dim formatter As New SoapFormatter()
formatter.Serialize(stream, obj)
stream.Close()
' Empties obj.
obj = Nothing
' Opens file "data.xml" and deserializes the object from it.
stream = File.Open("data.xml", FileMode.Open)
formatter = New SoapFormatter()
obj = CType(formatter.Deserialize(stream), TestSimpleObject)
stream.Close()
Console.WriteLine("")
Console.WriteLine("After deserialization the object contains: ")
obj.Print()
End Sub
End Class
' A test object that needs to be serialized.
<Serializable()> Public Class TestSimpleObject
Public member1 As Integer
Public member2 As String
Public member3 As String
Public member4 As Double
' A member that is not serialized.
<NonSerialized()> Public member5 As String
Public Sub New()
member1 = 11
member2 = "hello"
member3 = "hello"
member4 = 3.14159265
member5 = "hello world!"
End Sub
Public Sub Print()
Console.WriteLine("member1 = '{0}'", member1)
Console.WriteLine("member2 = '{0}'", member2)
Console.WriteLine("member3 = '{0}'", member3)
Console.WriteLine("member4 = '{0}'", member4)
Console.WriteLine("member5 = '{0}'", member5)
End Sub
End Class