Export (0) Print
Expand All

StreamingContext Constructor (StreamingContextStates, Object)

Initializes a new instance of the StreamingContext class with a given context state, and some additional information.

Namespace:  System.Runtime.Serialization
Assembly:  mscorlib (in mscorlib.dll)

public StreamingContext(
	StreamingContextStates state,
	Object additional
)

Parameters

state
Type: System.Runtime.Serialization.StreamingContextStates
A bitwise combination of the StreamingContextStates values that specify the source or destination context for this StreamingContext.
additional
Type: System.Object
Any additional information to be associated with the StreamingContext. This information is available to any object that implements ISerializable or any serialization surrogate. Most users do not need to set this parameter.

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));            
        }
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft