winform中有没有类似webform中的session的东西?

misssdf 2010-06-08 11:07:38
如题,我想用它保存一个值,比如说用户的权限。这个东西怎么使用。
或者说如果没有这种东西,有没有其他的方法来保存这个值,全局变量么?
...全文
116 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ny-6000 2010-06-08
  • 打赏
  • 举报
回复
有全局,静态变量的.
wuyq11 2010-06-08
  • 打赏
  • 举报
回复
没有,全局变量
XML,ini等文件
C/S是一直保持状态的
http://topic.csdn.net/u/20071109/11/fd9aec31-04ef-4cd8-a965-116dec01ecac.html
  • 打赏
  • 举报
回复
用XML也行啊
满衣兄 2010-06-08
  • 打赏
  • 举报
回复
可以写个单例模式的类来保存全局的东西。

Singleton

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

namespace Pattern.Singleton
{
  /**//// <summary>
  /// 泛型实现单例模式
  /// </summary>
  /// <typeparam name="T">需要实现单例的类</typeparam>
  public class Singleton<T> where T : new()
  {
    /**//// <summary>
    /// 返回类的实例
    /// </summary>
    public static T Instance
    {
      get { return SingletonCreator.instance; }
    }

    class SingletonCreator
    {
      internal static readonly T instance = new T();
    }
  }
}

Test

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Pattern.Singleton;

public partial class Singleton : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    // 使用单例模式,保证一个类仅有一个实例
    Response.Write(Singleton<Test>.Instance.Time);
    Response.Write("<br />");
    Response.Write(Singleton<Test>.Instance.Time);
    Response.Write("<br />");

    // 不用单例模式
    Test t = new Test();
    Response.Write(t.Time);
    Response.Write("<br />");
    Test t2 = new Test();
    Response.Write(t2.Time);
    Response.Write("<br />");
  }
}

public class Test
{
  private DateTime _time;

  public Test()
  {
    System.Threading.Thread.Sleep(3000);
    _time = DateTime.Now;  
  }

  public string Time
  {
    get { return _time.ToString(); }
  }
}


运行结果

2007-2-10 22:35:11
2007-2-10 22:35:11
2007-2-10 22:35:14
2007-2-10 22:35:17

参考

http://www.dofactory.com/Patterns/PatternSingleton.aspx
满衣兄 2010-06-08
  • 打赏
  • 举报
回复
application

1,979

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧