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


VB.NET StreamingContext構造函數代碼示例

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


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

示例1: Program

' 導入命名空間
Imports System.Text
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports System.IO
Imports System.Security.Permissions


Class Program
    
    Shared Sub Main(ByVal args() As String) 
        Try
            SerializeAndDeserialize()
        Catch exc As System.Exception
            Console.WriteLine(exc.Message)
        Finally
            Console.WriteLine("Press <Enter> to exit....")
            Console.ReadLine()
        End Try
    
    End Sub 
    
    Shared Sub SerializeAndDeserialize() 
        Dim myObject As Object = DateTime.Now
        ' Create a StreamingContext that includes a
        ' a DateTime. 
        Dim sc As New StreamingContext(StreamingContextStates.CrossProcess, myObject)
        Dim bf As New BinaryFormatter(Nothing, sc)
        Dim ms As New MemoryStream(New Byte(2047) {})
        bf.Serialize(ms, New [MyClass]())
        ms.Seek(0, SeekOrigin.Begin)
        Dim f As [MyClass] = CType(bf.Deserialize(ms), [MyClass])
        Console.WriteLine(vbTab + " MinValue: {0} " + vbLf + vbTab + " MaxValue: {1}", f.MinValue, f.MaxValue)
        Console.WriteLine("StreamingContext.State: {0}", sc.State)
        
        Dim myDateTime As DateTime = CType(sc.Context, DateTime)
        Console.WriteLine("StreamingContext.Context: {0}", myDateTime.ToLongTimeString())
    
    End Sub 
End Class 

<Serializable(), SecurityPermission(SecurityAction.Demand, SerializationFormatter := True)>  _
Class [MyClass]
    Implements ISerializable
    Private minValue_value As Integer
    Private maxValue_value As Integer
    
    Public Sub New() 
        minValue_value = Integer.MinValue
        maxValue_value = Integer.MaxValue    
    End Sub     
    
    Public Property MinValue() As Integer 
        Get
            Return minValue_value
        End Get
        Set
            minValue_value = value
        End Set
    End Property 
    
    Public Property MaxValue() As Integer 
        Get
            Return maxValue_value
        End Get
        Set
            maxValue_value = value
        End Set
    End Property
     
    Sub GetObjectData(ByVal si As SerializationInfo, ByVal context As StreamingContext)  Implements ISerializable.GetObjectData
        si.AddValue("minValue", minValue_value)
        si.AddValue("maxValue", maxValue_value)
    
    End Sub   
    
    Protected Sub New(ByVal si As SerializationInfo, ByVal context As StreamingContext) 
        minValue_value = Fix(si.GetValue("minValue", GetType(Integer)))
        maxValue_value = Fix(si.GetValue("maxValue", GetType(Integer)))
    End Sub 
End Class
開發者ID:VB.NET開發者,項目名稱:System.Runtime.Serialization,代碼行數:81,代碼來源:StreamingContext


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