ContextStack 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供堆栈对象,序列化程序可使用该对象向嵌套的序列化程序提供信息。
public ref class ContextStack sealedpublic sealed class ContextStacktype ContextStack = classPublic NotInheritable Class ContextStack- 继承
- 
				ContextStack
示例
下面的代码示例演示如何使用 ContextStack 推送然后删除 10 个值。
#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 );
}
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Windows.Forms;
namespace ContextStackExample
{
    class ContextStackExample
    {
        [STAThread]
        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() );
        }
    }
}
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()))
            stack.Push(number)
        Next number
        ' Pop each item off the stack.        
        Dim item As Object = stack.Pop()
        While item IsNot Nothing
            Console.WriteLine(("Value popped from stack: " + item.ToString()))
            item = stack.Pop()
        End While
    End Sub
End Module
注解
部分序列化程序需要有关对象的上下文的信息,以便正确地保留其状态。 类 ContextStack 使序列化程序能够设置有关要序列化到另一个序列化程序可以访问的堆栈的对象的上下文的数据。 属性的值 Context 由 提供, IDesignerSerializationManager 用于向某些序列化程序共享使用的信息。
上下文堆栈很有用,因为序列化设计文档的过程可以深入嵌套,并且每个嵌套级别的对象可能需要上下文信息才能正确保留对象的状态。 序列化程序可以在调用嵌套序列化程序之前将上下文对象设置为堆栈。 在调用嵌套序列化程序返回后,设置为堆栈的每个对象都应由设置它的序列化程序删除。
通常,堆栈上的对象包含有关正在序列化的当前对象的上下文的信息。 父序列化程序将有关下一个要序列化的对象的上下文信息添加到堆栈中,调用相应的序列化程序,当序列化程序完成对对象执行时,将从堆栈中删除上下文信息。 由每个序列化程序的实现来确定在此堆栈上推送哪些对象。
例如,具有名为 Enabled 的属性的对象的数据类型为 Boolean。 如果序列化程序将此值写入数据流,则它可能需要包含其正在写入的属性的上下文或类型。 但是,序列化程序没有此信息,因为它只指示写入 Boolean 值。 若要向序列化程序提供此信息,父序列化程序可以推送 PropertyDescriptor 指向 Enabled 上下文堆栈上的 属性的 。
构造函数
| ContextStack() | 初始化 ContextStack 类的新实例。 | 
属性
| Current | 获取堆栈上的当前对象。 | 
| Item[Int32] | 获取堆栈上指定级别的对象。 | 
| Item[Type] | 获取堆栈上从指定类型继承或者实现指定类型的第一个对象。 | 
方法
| Append(Object) | 将对象追加到堆栈的结尾,而不是将其推到堆栈的顶部。 | 
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| Pop() | 从堆栈中移除当前对象并返回其值。 | 
| Push(Object) | 将指定对象推入(即放置)到堆栈上。 | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) |