語言

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 的會話項目集合。

ISessionStateItemCollection 介面的 ASP.NET 實作是 SessionStateItemCollection 類別。

如果你從該 SessionStateStoreProviderBase 類別衍生出一個類別來儲存會話資料,你可以用該 SessionStateItemCollection 類別來管理儲存物件,或是在你自己的集合管理器中實作介面 ISessionStateItemCollection 。

如果你實作了介面, ISessionStateItemCollection 也必須建立一個繼承該 SessionStateStoreProviderBase 類別的類別,以便利用你的 ISessionStateItemCollection 實作來管理會話變數。

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

屬性

名稱 Description
Count

得到包含於 的 ICollection元素數量。

(繼承來源 ICollection)
Dirty

取得或設定一個值,表示集合是否被標記為變更。

IsSynchronized

取得值,指出是否同步存取 ICollection (線程安全)。

(繼承來源 ICollection)
Item[Int32]

透過數值索引取得或設定集合中的值。

Item[String]

透過名稱取得或設定集合中的值。

Keys

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

SyncRoot

取得一個物件,可用來同步存取 ICollection。

(繼承來源 ICollection)

方法

名稱 Description
Clear()

移除會話狀態集合中的所有值與鍵。

CopyTo(Array, Int32)

從特定ICollection索引開始,將 的Array元素複製到 Array。

(繼承來源 ICollection)
GetEnumerator()

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

(繼承來源 IEnumerable)
Remove(String)

從收藏中刪除一個項目。

RemoveAt(Int32)

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

擴充方法

名稱 Description
AsParallel(IEnumerable)

啟用查詢的平行處理。

AsQueryable(IEnumerable)

將 IEnumerable 轉換成 IQueryable。

Cast<TResult>(IEnumerable)

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

OfType<TResult>(IEnumerable)

根據指定的型別篩選 IEnumerable 的專案。

適用於

另請參閱