This documentation is archived and is not being maintained.

ContextStack Class

Provides a stack object that can be used by a serializer to make information available to nested serializers.

For a list of all members of this type, see ContextStack Members.


[Visual Basic]
NotInheritable Public Class ContextStack
public sealed class ContextStack
public __gc __sealed class ContextStack
public class ContextStack

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


Some serializers require information about the context of an object to correctly persist their state. ContextStack enables a serializer to set data about the context of an object that is being serialized to a stack where another serializer can access it. The Context object is provided by an IDesignerSerializationManager to share information of use to some serializers.

A context stack is useful because the process of serializing a design document can be deeply nested, and objects at each level of nesting may require context information to correctly persist the state of the object. A serializer can set a context object to the stack before invoking a nested serializer. Each object set to the stack should be removed by the serializer that set it after a call to a nested serializer returns.

Typically, the objects on the stack contain information about the context of the current object that is being serialized. A parent serializer adds context information to the stack about the next object to be serialized, calls an appropriate serializer, and when the serializer finishes executing on the object, removes the context information from the stack. It is up to the implementation of each serializer to determine what objects get pushed on this stack.

As an example, an object with a property named Enabled has a data type of Boolean. If a serializer writes this value to a data stream, it might need to include the context or type of property it is writing. The serializer does not have this information, however, because it is only instructed to write the Boolean value. To provide this information to the serializer, the parent serializer can push a PropertyDescriptor that points to the Enabled property on the context stack.


[Visual Basic, C#, C++] The following code example demonstrates using a ContextStack to push and then pop ten values.

[Visual Basic] 
Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.ComponentModel.Design.Serialization

Module ContextStackExample

    Sub Main()
        ' Create a ContextStack.
        Dim stack As New ContextStack

        ' Push ten items on to the stack and output the value of each.
        Dim number As Integer
        For number = 0 To 9
            Console.WriteLine(("Value pushed to stack: " + number.ToString()))
        Next number

        ' Pop each item off the stack.        
        Dim item As Object = stack.Pop()
        While Not item Is Nothing
            Console.WriteLine(("Value popped from stack: " + item.ToString()))
            item = stack.Pop()
        End While
    End Sub

End Module

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Windows.Forms;

namespace ContextStackExample
    class ContextStackExample
        static void Main(string[] args)
            // Create a ContextStack.
            ContextStack stack = new ContextStack();
            // Push ten items on to the stack and output the value of each.
            for( int number = 0; number < 10; number ++ )
                Console.WriteLine( "Value pushed to stack: "+number.ToString() );
                stack.Push( number );

            // Pop each item off the stack.
            object item = null;
            while( (item = stack.Pop()) != null )
                Console.WriteLine( "Value popped from stack: "+item.ToString() );

#using <mscorlib.dll>
#using <System.Windows.Forms.dll>
#using <System.dll>
using namespace System;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::ComponentModel::Design::Serialization;
using namespace System::Windows::Forms;

int main()
   // Create a ContextStack.
   ContextStack* stack = new ContextStack();

   // Push ten items on to the stack and output the value of each.
   for( int number = 0; number < 10; number ++ )
      Console::WriteLine( S"Value pushed to stack: {0}", __box(number));

   // Pop each item off the stack.
   Object* item = 0;
   while( (item = stack->Pop()) != 0 )
      Console::WriteLine( S"Value popped from stack: {0}", item );

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.ComponentModel.Design.Serialization

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

ContextStack Members | System.ComponentModel.Design.Serialization Namespace | IDesignerSerializationManager