• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
works guo

成功的渴望与生俱来---- 成功是获得赞美与尊重的最有效的途径
博客园    首页    新随笔    联系   管理    订阅  订阅

SessionStateItemCollection


SessionStateItemCollection class


一个被存储在session State中的对象集.这个类不能被继承.

SessionStateItemCollection 类是使用来管理session-state变量值的索引,这个索引是按照变量名或index.这个session-state变量被APS.NET应用程序使用在HttpSessionState 类,如要访问就使用HttpContext或Page的Session属性,这个HttpSessionState类调用HttpSessionStateContainer 类来管理被SessionStateItemCollection使用的session-state变量值.

注意实现:

当创建一个自定义session-state提供者基类SesssionStateStoreProviderBase类时,使用SessionStateItemCollection类管理每一项被存储session-state变量值,如果你需要创建自定义对象管理sessio-state items,就要实现ISessionStateItemCollection interface.

下面是创建一个新的SessionStateItemCollection 对象和使用set和get得到变量值.

SessionStateItemCollection items = new SessionStateItemCollection();

items[
"LastName"] = "Wilson";
items[
"FirstName"] = "Dan";

foreach (string s in items.Keys)
  Response.Write(
"items[\"" + s + "\"] = " + items[s].ToString() + "<br />");



上面说到如果要自定义就要实现下面这个东东


ISessionStateItemCollection Interface


为ASP.NET session state使用集合去管理session定义契约.

[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level 
= AspNetHostingPermissionLevel.Minimal)]
public interface ISessionStateItemCollection : ICollection,
    IEnumerable

ISessionStateItemCollection interface 定义session items 的collection暴露给应用程序代码HttpSessionStateContainer Class.

这个ASP.NET实现ISessionStateItemCollection interface 的类是SessionStateItemCollection class.

如果 你创建一个类派生SessionStateStoreProviderBase 类去存储Session数据,你既能使用SessionStateItemCollection class去管理存储对象也可以自己实现ISessionStateItemCollection interface来管理.

如果你实现了ISessionStateItemCollection interface,你必须创建一个类继承SessionStateStoreProviderBase 为产生你的ISessionStateItemCollection 实现管理session变量而使用.

一个ISessionStateItemCollection实现必须实现ICollection interface的成员.

下面是实现ISessionStateItemCollection使用SortedList类来存储session-State变量名和值.

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


  }


}

相关类的信息


Wokrs guo
posted @ 2008-03-20 12:53  works guo  阅读(627)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3