ISessionStateItemCollection 介面

定義

定義 ASP.NET 工作階段狀態用來管理工作階段的集合所使用的合約。

public interface class ISessionStateItemCollection : System::Collections::ICollection
public interface ISessionStateItemCollection : System.Collections.ICollection
type ISessionStateItemCollection = interface
    interface ICollection
    interface IEnumerable
Public Interface ISessionStateItemCollection
Implements ICollection
衍生
實作

範例

下列程式碼範例會實作 ISessionStateItemCollection ,並使用 SortedList 類別來儲存會話狀態變數名稱和值。

using System;
using System.Web;
using System.Web.SessionState;
using System.Collections;
using System.Collections.Specialized;

namespace Samples.AspNet.Session
{

  public class MySessionStateItemCollection : ISessionStateItemCollection
  {
    private SortedList pItems = new SortedList();
    private bool pDirty = false;

    public bool Dirty
    {
      get { return pDirty; }
      set { pDirty = value; }
    }

    public object this[int index]
    {
      get { return pItems[index]; }
      set
      {
        pItems[index] = value;
        pDirty = true;
      }
    }

    public object this[string name]
    {
      get { return pItems[name]; }
      set
      {
        pItems[name] = value;
        pDirty = true;
      }
    }

    public NameObjectCollectionBase.KeysCollection Keys
    {
      get { return (NameObjectCollectionBase.KeysCollection)pItems.Keys; }
    }

    public int Count
    {
      get { return pItems.Count; }
    }

    public Object SyncRoot
    {
      get { return this; }
    }

    public bool IsSynchronized
    {
      get { return false; }
    }

    public IEnumerator GetEnumerator()
    {
      return pItems.GetEnumerator(); 
    }

    public void Clear()
    {
      pItems.Clear();
      pDirty = true;
    }

    public void Remove(string name)
    {
      pItems.Remove(name);
      pDirty = true;
    }

    public void RemoveAt(int index)
    {
      if (index < 0 || index >= this.Count)
        throw new ArgumentOutOfRangeException("The specified index is not within the acceptable range.");

      pItems.RemoveAt(index);
      pDirty = true;
    }

    public void CopyTo(Array array, int index)
    {
      pItems.CopyTo(array, index);
    }
  }
}
Imports System.Web
Imports System.Web.SessionState
Imports System.Collections
Imports System.Collections.Specialized


Namespace Samples.AspNet.Session

  Public Class MySessionStateItemCollection
    Implements ISessionStateItemCollection
  
    Private pItems As SortedList = New SortedList()
    Private pDirty As Boolean = False

    Public Property Dirty As Boolean Implements ISessionStateItemCollection.Dirty    
      Get
        Return pDirty
      End Get
      Set
        pDirty = value
      End Set
    End Property

    Public Property Item(index As Integer) As Object Implements ISessionStateItemCollection.Item
      Get
        Return pItems(index)
      End Get
      Set
        pItems(index) = value
        pDirty = True
      End Set
    End Property

    Public Property Item(name As String) As Object Implements ISessionStateItemCollection.Item
      Get
        Return pItems(name)
      End Get
      Set
        pItems(name) = value
        pDirty = True
      End Set
    End Property

    Public ReadOnly Property Keys As NameObjectCollectionBase.KeysCollection _
      Implements ISessionStateItemCollection.Keys
      Get
        Return CType(pItems.Keys, NameObjectCollectionBase.KeysCollection)
      End Get
    End Property

    Public ReadOnly Property Count As Integer Implements ICollection.Count    
      Get
        Return pItems.Count
      End Get
    End Property

    Public ReadOnly Property SyncRoot As Object Implements ICollection.SyncRoot    
      Get
        Return Me
      End Get
    End Property

    Public ReadOnly Property IsSynchronized As Boolean Implements ICollection.IsSynchronized
      Get
        Return False
      End Get
    End Property

    Public Function GetEnumerator() As IEnumerator Implements ICollection.GetEnumerator    
      Return pItems.GetEnumerator() 
    End Function

    Public Sub Clear() Implements ISessionStateItemCollection.Clear
      pItems.Clear()
      pDirty = True
    End Sub

    Public Sub Remove(name As String) Implements ISessionStateItemCollection.Remove
      pItems.Remove(name)
      pDirty = True
    End Sub

    Public Sub RemoveAt(index As Integer) Implements ISessionStateItemCollection.RemoveAt 
      If index < 0 OrElse index >= Me.Count Then _
        Throw New ArgumentOutOfRangeException("The specified index is not within the acceptable range.")
   
      pItems.RemoveAt(index)
      pDirty = True
    End Sub

    Public Sub CopyTo(array As Array, index As Integer) Implements ICollection.CopyTo
      pItems.CopyTo(array, index)
    End Sub

  End Class

End Namespace

備註

介面 ISessionStateItemCollection 會定義 類別公開給應用程式程式碼的 HttpSessionStateContainer 會話專案集合。

介面的 ASP.NET 實作 ISessionStateItemCollectionSessionStateItemCollection 類別。

如果您建立衍生自 類別的 SessionStateStoreProviderBase 類別來儲存會話資料,您可以使用 SessionStateItemCollection 類別來管理預存物件,或在您自己的集合管理員上實 ISessionStateItemCollection 作 介面。

如果您實作 ISessionStateItemCollection 介面,您也必須建立繼承 類別的 SessionStateStoreProviderBase 類別,才能使用實 ISessionStateItemCollection 作來管理會話變數。

ISessionStateItemCollection 作也必須實作 介面的成員 ICollection

屬性

Count

取得 ICollection 中所包含的項目數。

(繼承來源 ICollection)
Dirty

取得或設定值,這個值表示集合是否標示為已變更。

IsSynchronized

取得值,這個值表示對 ICollection 的存取是否同步 (安全執行緒)。

(繼承來源 ICollection)
Item[Int32]

根據數字索引取得或設定集合中的值。

Item[String]

根據名稱取得或設定集合中的值。

Keys

取得由集合中所有值的變數名稱組成的集合。

SyncRoot

取得可用以同步存取 ICollection 的物件。

(繼承來源 ICollection)

方法

Clear()

將工作階段狀態集合中的所有值和索引鍵全部移除。

CopyTo(Array, Int32)

從特定的 ICollection 索引開始,將 Array 的項目複製到 Array

(繼承來源 ICollection)
GetEnumerator()

傳回逐一查看集合的列舉值。

(繼承來源 IEnumerable)
Remove(String)

從集合中刪除項目。

RemoveAt(Int32)

從集合中刪除指定索引處的項目。

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

另請參閱