ISessionStateItemCollection 接口

定义

为 ASP.NET 会话状态使用的集合定义协定,以管理会话。

C#
public interface ISessionStateItemCollection : System.Collections.ICollection
派生
实现

示例

下面的代码示例实现 ISessionStateItemCollection 并使用 SortedList 类来存储会话状态变量名称和值。

C#
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);
    }
  }
}

注解

接口 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

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另请参阅