Export (0) Print
Expand All

StreamingContext.Context Property

Gets context specified as part of the additional context.

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

public Object Context { get; }

Property Value

Type: System.Object
The context specified as part of the additional context.

Can be null.

The following example creates an instance of the StreamingContext class using the state and additional parameters. The code also shows the deserialization of an object and retrieving the value of the Context property.

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.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft