當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET IDeserializationCallback.OnDeserialization方法代碼示例

本文整理匯總了VB.NET中System.Runtime.Serialization.IDeserializationCallback.OnDeserialization方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET IDeserializationCallback.OnDeserialization方法的具體用法?VB.NET IDeserializationCallback.OnDeserialization怎麽用?VB.NET IDeserializationCallback.OnDeserialization使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Runtime.Serialization.IDeserializationCallback的用法示例。


在下文中一共展示了IDeserializationCallback.OnDeserialization方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: New

' 導入命名空間
Imports System.IO
Imports System.Collections
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization

' This class is serializable and will have its OnDeserialization method
' called after each instance of this class is deserialized.
<Serializable()> Class Circle
   Implements IDeserializationCallback
   Private m_radius As Double

   ' To reduce the size of the serialization stream, the field below is 
   ' not serialized. This field is calculated when an object is constructed
   ' or after an instance of this class is deserialized.
   <NonSerialized()> Public m_area As Double

   Public Sub New(ByVal radius As Double)
      m_radius = radius
      m_area = Math.PI * radius * radius
   End Sub

   Private Sub OnDeserialization(ByVal sender As Object) _
      Implements IDeserializationCallback.OnDeserialization
      ' After being deserialized, initialize the m_area field 
      ' using the deserialized m_radius value.
      m_area = Math.PI * m_radius * m_radius
   End Sub

   Public Overrides Function ToString() As String
      Return String.Format("radius={0}, area={1}", m_radius, m_area)
   End Function
End Class


Class Class1
   <STAThread()> Shared Sub Main()
      Serialize()
      Deserialize()
   End Sub

   Shared Sub Serialize()
      Dim c As New Circle(10)
      Console.WriteLine("Object being serialized: " + c.ToString())

      ' To serialize the Circle, you must first open a stream for 
      ' writing. Use a file stream here.
      Dim fs As New FileStream("DataFile.dat", FileMode.Create)

      ' Construct a BinaryFormatter and use it 
      ' to serialize the data to the stream.
      Dim formatter As New BinaryFormatter
      Try
         formatter.Serialize(fs, c)
      Catch e As SerializationException
         Console.WriteLine("Failed to serialize. Reason: " + e.Message)
         Throw
      Finally
         fs.Close()
      End Try
   End Sub


   Shared Sub Deserialize()
      ' Declare the Circle reference
      Dim c As Circle = Nothing

      ' Open the file containing the data that you want to deserialize.
      Dim fs As New FileStream("DataFile.dat", FileMode.Open)
      Try
         Dim formatter As New BinaryFormatter

         ' Deserialize the Circle from the file and 
         ' assign the reference to the local variable.
         c = CType(formatter.Deserialize(fs), Circle)
      Catch e As SerializationException
         Console.WriteLine("Failed to deserialize. Reason: " + e.Message)
         Throw
      Finally
         fs.Close()
      End Try

      ' To prove that the Circle deserialized correctly, display its area.
      Console.WriteLine("Object being deserialized: " + c.ToString())
   End Sub
End Class
開發者ID:VB.NET開發者,項目名稱:System.Runtime.Serialization,代碼行數:86,代碼來源:IDeserializationCallback.OnDeserialization


注:本文中的System.Runtime.Serialization.IDeserializationCallback.OnDeserialization方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。