C # Syntax sugar session operation class ASP.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.