ContextStack Class
Provides a stack object that can be used by a serializer to make information available to nested serializers.
Assembly: System (in System.dll)
The ContextStack type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | Current | Gets the current object on the stack. |
![]() | Item[Int32] | Gets the object on the stack at the specified level. |
![]() | Item[Type] | Gets the first object on the stack that inherits from or implements the specified type. |
| Name | Description | |
|---|---|---|
![]() | Append | Appends an object to the end of the stack, rather than pushing it onto the top of the stack. |
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | Pop | Removes the current object off of the stack, returning its value. |
![]() | Push | Pushes, or places, the specified object onto the stack. |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
Some serializers require information about the context of an object to correctly persist their state. The ContextStack class 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 value of the Context property 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.
Note |
|---|
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: SharedState. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes. |
The following code example demonstrates using a ContextStack to push and then remove 10 values.
#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 = gcnew 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: {0}", number ); stack->Push( number ); } // Pop each item off the stack. Object^ item = nullptr; while ( (item = stack->Pop()) != 0 ) Console::WriteLine( "Value popped from stack: {0}", item ); }
- NamedPermissionSet
for full access to system resources. Demand values: LinkDemand, InheritanceDemand. Associated state: FullTrust
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.
