StreamingContext StreamingContext StreamingContext StreamingContext Constructors

定義

多載

StreamingContext(StreamingContextStates) StreamingContext(StreamingContextStates) StreamingContext(StreamingContextStates) StreamingContext(StreamingContextStates)

使用指定的內容狀態,初始化 StreamingContext 類別的新執行個體。Initializes a new instance of the StreamingContext class with a given context state.

StreamingContext(StreamingContextStates, Object) StreamingContext(StreamingContextStates, Object) StreamingContext(StreamingContextStates, Object) StreamingContext(StreamingContextStates, Object)

使用指定的內容狀態和某些額外的資訊,初始化 StreamingContext 類別的新執行個體。Initializes a new instance of the StreamingContext class with a given context state, and some additional information.

StreamingContext(StreamingContextStates) StreamingContext(StreamingContextStates) StreamingContext(StreamingContextStates) StreamingContext(StreamingContextStates)

使用指定的內容狀態,初始化 StreamingContext 類別的新執行個體。Initializes a new instance of the StreamingContext class with a given context state.

public:
 StreamingContext(System::Runtime::Serialization::StreamingContextStates state);
public StreamingContext (System.Runtime.Serialization.StreamingContextStates state);
new System.Runtime.Serialization.StreamingContext : System.Runtime.Serialization.StreamingContextStates -> System.Runtime.Serialization.StreamingContext
Public Sub New (state As StreamingContextStates)

參數

state
StreamingContextStates StreamingContextStates StreamingContextStates StreamingContextStates

StreamingContextStates 數值的位元組合,指定這個 StreamingContext 的來源或目的端內容。A bitwise combination of the StreamingContextStates values that specify the source or destination context for this StreamingContext.

另請參閱

StreamingContext(StreamingContextStates, Object) StreamingContext(StreamingContextStates, Object) StreamingContext(StreamingContextStates, Object) StreamingContext(StreamingContextStates, Object)

使用指定的內容狀態和某些額外的資訊,初始化 StreamingContext 類別的新執行個體。Initializes a new instance of the StreamingContext class with a given context state, and some additional information.

public:
 StreamingContext(System::Runtime::Serialization::StreamingContextStates state, System::Object ^ additional);
public StreamingContext (System.Runtime.Serialization.StreamingContextStates state, object additional);
new System.Runtime.Serialization.StreamingContext : System.Runtime.Serialization.StreamingContextStates * obj -> System.Runtime.Serialization.StreamingContext
Public Sub New (state As StreamingContextStates, additional As Object)

參數

state
StreamingContextStates StreamingContextStates StreamingContextStates StreamingContextStates

StreamingContextStates 數值的位元組合,指定這個 StreamingContext 的來源或目的端內容。A bitwise combination of the StreamingContextStates values that specify the source or destination context for this StreamingContext.

additional
Object Object Object Object

StreamingContext 相關聯的任何額外資訊。Any additional information to be associated with the StreamingContext. 任何實作 ISerializable 的物件或任何序列化 (Serialization) 代理都可以使用這項資訊。This information is available to any object that implements ISerializable or any serialization surrogate. 大部分的使用者不需要設定這個參數。Most users do not need to set this parameter.

範例

下列範例會StreamingContext state使用和additional參數來建立類別的實例。The following example creates an instance of the StreamingContext class using the state and additional parameters.

using System;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
using System.Security.Permissions;

namespace StreamingContextExample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                SerializeAndDeserialize();
            }
            catch (System.Exception exc)
            {
                Console.WriteLine(exc.Message);
            }
            finally
            {
                Console.WriteLine("Press <Enter> to exit....");
                Console.ReadLine();
            }
        }
        static void SerializeAndDeserialize()
        {
            object myObject = DateTime.Now;
            // Create a StreamingContext that includes a
            // a DateTime. 
            StreamingContext sc = new StreamingContext(
                StreamingContextStates.CrossProcess, myObject);
            BinaryFormatter bf = new BinaryFormatter(null, sc);
            MemoryStream ms = new MemoryStream(new byte[2048]);
            bf.Serialize(ms, new MyClass());
            ms.Seek(0, SeekOrigin.Begin);
            MyClass f = (MyClass)bf.Deserialize(ms);
            Console.WriteLine("\t MinValue: {0} \n\t MaxValue: {1}", 
                f.MinValue , f.MaxValue);
            Console.WriteLine("StreamingContext.State: {0}", 
                sc.State);

            DateTime myDateTime = (DateTime)sc.Context;
            Console.WriteLine("StreamingContext.Context: {0}",
                myDateTime.ToLongTimeString());
        }
    }

    [Serializable]
    [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
    class MyClass : ISerializable
    {
        private int minValue_value;
        private int maxValue_value;

        public MyClass()
        {
            minValue_value = int.MinValue;
            maxValue_value = int.MaxValue;
        }

        public int MinValue
        {
            get { return minValue_value; }
            set { minValue_value = value;}
        }

        public int MaxValue
        {
            get { return maxValue_value; }
            set { maxValue_value = value; }
        }

        void ISerializable.GetObjectData(SerializationInfo si, StreamingContext context)
        {
            si.AddValue("minValue", minValue_value);
            si.AddValue("maxValue", maxValue_value);
        }

        protected MyClass(SerializationInfo si, StreamingContext context)
        {
            minValue_value = (int)si.GetValue("minValue", typeof(int));
            maxValue_value = (int)si.GetValue("maxValue", typeof(int));            
        }
    }
}
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 
另請參閱

適用於