System.Runtime.Serialization.StreamingContext Structure

Assembly: Mscorlib.dll
Namespace: System.Runtime.Serialization
Summary
Describes the source and destination of a given serialized stream, as well as a means for serialization to retain that context and an additional caller-defined context.
C# Syntax:
[Serializable]
public struct StreamingContext
Remarks
Indicates the source or destination of the bits that the formatter is using. Classes with surrogates or implementing ISerializable can serialize or ignore fields and values based on the information stored in the streaming context. For example, a window handle is still valid if StreamingContext.State has the StreamingContext.State StreamingContextStates.CrossProcess value set, but not with the StreamingContextStates.CrossMachine or StreamingContextStates.File value set.
Example
The following code example demonstrates the StreamingContext struct.
 using System;
 using System.Runtime.Serialization;
 
 [Serializable()]
 public class MyClass: ISerializable {

    public static void Main() {}
 
    // A handle to a window that has meaning only at this time and on this computer.
    public int winhandle;
    public double value = 3.14159265;
 
    // Serialization constructor.
    public MyClass (SerializationInfo info, StreamingContext context)  {
 
       // If this is a serialization to another process, winhandle must be deserialized.
       if(context.State == StreamingContextStates.CrossProcess) {
          winhandle = (int)info.GetValue("winhandle", typeof(int));
       }
       else {
          // If this is a serialization from anywhere else, set winhandle to a default value.
          winhandle = -1;
    }
 
       // Deserializes the value.
       value = (double)info.GetValue("MyClass_value", typeof(double));
    }
 
    // Serializes the object.
    public void GetObjectData(SerializationInfo info, StreamingContext context) {  
       // Serializes the value.
       info.AddValue("MyClass_value", value);
 
       // If this object is going not to another process but to a different computer,
       // winhandle has no meaning, and does not need to be serialized.
       if(context.State == StreamingContextStates.CrossProcess) {
          info.AddValue("winhandle", winhandle);
       }
    }
 }

    
See also:
System.Runtime.Serialization Namespace | StreamingContext.State | MSDN: serialization

System.Runtime.Serialization.StreamingContext Member List:

Public Constructors
ctor #1 Overloaded:
.ctor(StreamingContextStates state)

Initializes a new instance of the StreamingContext class with a given context state.
ctor #2 Overloaded:
.ctor(StreamingContextStates state, object additional)

Initializes a new instance of the StreamingContext class with a given context state, and some additional information.
Public Properties
Context Read-only

Gets context specified as part of the additional context.
State Read-only

Gets the source or destination of the transmitted data.
Public Methods
Equals Overridden:
Determines whether two StreamingContext instances contain the same values.
GetHashCode Overridden:
Returns a hash code of this object.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToString
(inherited from System.ValueType)
See base class member description: System.ValueType.ToString


Returns the fully qualified type name of this instance.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Runtime.Serialization.StreamingContext Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the StreamingContext class with a given context state.
C# Syntax:
public StreamingContext(StreamingContext(
   StreamingContextStates state
);
Parameters:

state

A bitwise combination of the StreamingContextStates values that specify the source or destination context for this StreamingContext.

See also:
FlagsAttribute

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the StreamingContext class with a given context state, and some additional information.
C# Syntax:
public StreamingContext(StreamingContext(
   StreamingContextStates state,
   object additional
);
Parameters:

state

A bitwise combination of the StreamingContextStates values that specify the source or destination context for this StreamingContext.

additional

Any additional information to be associated with the StreamingContext. This information will be available to any object implementing ISerializable or any Serialization Surrogate. Most users will not need to set this parameter.

See also:
FlagsAttribute

Return to top


Property: Context (read-only)
Summary
Gets context specified as part of the additional context.
C# Syntax:
public object Context {get;}
Remarks
Can be null.

Return to top


Property: State (read-only)
Summary
Gets the source or destination of the transmitted data.
C# Syntax:
public StreamingContextStates State {get;}
Remarks
During serialization, the current property specifies the destination of the transmitted data. For example, when serializing data from Cross AppDomain Remoting, the StreamingContextStates will be StreamingContextStates.CrossProcess. During deserialization, the state indicates the source of the deserialized stream.

Return to top


Overridden Method: Equals(
   object obj
)
Summary
Determines whether two StreamingContext instances contain the same values.
C# Syntax:
public override bool Equals(
   object obj
);
Parameters:

obj

An object to compare with the current instance.

Return Value:
true if the specified object is an instance of StreamingContext and equals the value of the current instance; otherwise, false.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~StreamingContext();

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: GetHashCode()
Summary
Returns a hash code of this object.
C# Syntax:
public override int GetHashCode();
Return Value:
The StreamingContextStates value that contains the source or destination of the serialization for this StreamingContext.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.ValueType.ToString

Summary
Returns the fully qualified type name of this instance.
C# Syntax:
public override string ToString();
Return Value:
A String containing a fully qualified type name.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.