`
guojingxf
  • 浏览: 65989 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

c#中实现全局数据变量

阅读更多

C#中没有session 可以自己写方法

 

 

using System;
using System.Collections.Generic;
using System.Text;

namespace JXC69IC
{
    class SingletonTemplate<T>
    {
        private static object locker = new object();
        private static SingletonTemplate<T> obj = null;
        private static T instance = default(T);

        private SingletonTemplate()
        {
            instance = Activator.CreateInstance<T>();
        }

        public static T Instance
        {
            get
            {
                lock (locker)
                {
                    if (obj == null)
                    {
                        obj = new SingletonTemplate<T>();
                    }
                }
                return instance;
            }
        }


    }
}
首先初始化数据

 

 //全局保存用户信息
                SingletonTemplate<Models.JxcCuser>.Instance.USERNAME=jc.USERNAME ;//用户名
                SingletonTemplate<Models.JxcCuser>.Instance.USERFT=jc.USERFT;//密码
                SingletonTemplate<Models.JxcCuser>.Instance.USERPWD=jc.USERPWD;//权限

调用获取数据

//获取用户权限信息
            string userft = SingletonTemplate<Models.JxcCuser>.Instance.USERFT.ToString();
            string username = SingletonTemplate<Models.JxcCuser>.Instance.USERNAME.ToString();

 

ok其中的单例模式 就不多说了

 

 

  • 大小: 33.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics