ContextStack 類別

定義

提供堆疊物件,序列化工具可以使用這個堆疊物件,使資訊可以為巢狀序列化工具所用。

public ref class ContextStack sealed
public sealed class ContextStack
type ContextStack = class
Public 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

備註

某些序列化程式會要求有關物件內容的資訊,以便正確保存 (Persist) 它們的狀態。 類別 ContextStack 可讓串行化程式設定物件內容的相關數據,而該物件正在串行化至堆疊,讓另一個串行化程式可以存取它。 屬性的值 Context 是由 提供 IDesignerSerializationManager ,以共用一些串行化程式所使用的資訊。

內容堆疊很有用,因為串行化設計檔的程式可以深入巢狀,而且每個巢狀層級的物件可能需要內容資訊才能正確保存物件的狀態。 串行化程式可以在叫用巢狀串行化程式之前,將內容物件設定為堆疊。 在呼叫巢狀串行化程式傳回之後,串行化程式應該移除設定堆疊的每個物件。

一般而言,堆疊上的物件包含目前物件正在串行化之內容的相關信息。 父串行化程式會將要串行化之下一個物件的內容資訊新增至堆疊、呼叫適當的串行化程式,以及在物件上執行時,從堆疊中移除內容資訊。 每個串行化程序的實作都是要決定在此堆疊上推送的物件。

例如,具有名為 Enabled 之屬性的物件具有 的數據類型為 Boolean。 如果串行化程式將此值寫入數據流,可能需要包含正在寫入的內容或屬性類型。 不過,串行化程序沒有這項資訊,因為它只會指示寫入 Boolean 值。 若要將這項資訊提供給串行化程式,父串行化程式可以將 指向Enabled內容堆疊上 屬性的 推送PropertyDescriptor

建構函式

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)

適用於

另請參閱