Usage:
Fame a data set var liststring = new list<string> () {"A", "B", "C"}; Session key String key = "Sekey"; Get instance var sessionmanager = Sessionmanager<list<string>>. GetInstance (); Add session Sessionmanager.add (key, liststring); Add has other overloads above is the most basic //Get list<string> sessionlist = Sessionmanager[key]; Other methods Sessionmanager.containskey (key); Sessionmanager.remove (key);//delete Sessionmanager.removeall (c = c.contains ("Sales_"));//delete key contains Sales_ Session Sessionmanager.getallkey ();//Get all keys
Code:
Using system;using system.collections.generic;using system.linq;using system.text;using System.Web;namespace syntacticsugar{//<summary>//* Description: Session operation class//* * Founding Date: 2015-6-9//* * Modified:-//* * Sunkaixu AN//* * Instructions for use://</summary>//<typeparam name= "K" > Key </typeparam>//<typeparam name= "V" > Value </typeparam> public class Sessionmanager<v>: ihttpstorageobject<v> {private Stati C ReadOnly Object _instancelock = new Object (); private static sessionmanager<v> _instance = null; public static sessionmanager<v> getinstance () {if (_instance = = null) { Lock (_instancelock) {if (_instance = = null) { _instance = new sessionmanager<v> (); }}} return _instance; } public override void ADD (string key, V value) {context. Session.add (key, value); } public override bool ContainsKey (string key) {return context. Session[key]! = NULL; public override V Get (string key) {return (V) context. Session[key]; } public override ienumerable<string> Getallkey () {foreach (var key in context). Session.keys) {yield return key. ToString (); }} public override void Remove (string key) {context. Session[key] = null; Context. Session.remove (key); } public override void RemoveAll () {foreach (var key in Getallkey ()) { Remove (key); }} public override void RemoveAll (func<string, bool> removeexpression) {var allkey List = Getallkey (). ToList (); var removekeylist = Allkeylist.where (removeexpression). ToList (); foreach (var key in removekeylist) {Remove (key); }} public override V This[string key] {get {return (V) context. Session[key]; } } }}
Using System;namespace syntacticsugar{public abstract class ihttpstorageobject<v> {public int Minutes =; public int Hour = *; public int day = * *; Public System.Web.HttpContext context = System.Web.HttpContext.Current; public abstract void Add (string key, V value); public abstract bool ContainsKey (string key); Public abstract V Get (string key); Public abstract global::system.collections.generic.ienumerable<string> Getallkey (); public abstract void Remove (string key); public abstract void RemoveAll (); public abstract void RemoveAll (func<string, bool> removeexpression); Public abstract V this[string key] {get;}} }
C # Syntax sugar session operation class ASP.