4

これを検索してみましたが、検索用にどのように表現すればよいかさえわかりません。

私がやろうとしているのは、クラスにアクセスして変更するたびに、実際にセッションから値を取得して設定するクラスを持つことです。

これが私がやろうとしていることです(私がこれまでに持っているものです。):

public class example
{
   public int prop1 {get;set;}

   public static example Instance
   {
       return (example)(HttpContext.Current.Session["exampleClass"] ?? new example());
   }

}

public class main
{
   protected void Page_Load(object sender, EventArgs e)
   {
      example.Instance.prop1 = "aaa"; //stores value into session
      txtInput.Text = example.Instance.prop1; //retrieves value from session
   }
}

私がやろうとしていることについて、それが理にかなっていることを願っています。

どんな助けでも大歓迎です、ありがとう。

4

5 に答える 5

6

これは、ジェネリックを使用すると簡単に実行できます。

これを試してみてください。

public class Session
{
    public User User
    {
        get { return Get<User>("User"); }
        set {Set<User>("User", value);}
    }

    /// <summary> Gets. </summary>
    /// <typeparam name="T"> Generic type parameter. </typeparam>
    /// <param name="key"> The key. </param>
    /// <returns> . </returns>
    private T Get<T>(string key)
    {
        object o = HttpContext.Current.Session[key];
        if(o is T)
        {
            return (T) o;
        }

        return default(T);
    }

    /// <summary> Sets. </summary>
    /// <typeparam name="T"> Generic type parameter. </typeparam>
    /// <param name="key">  The key. </param>
    /// <param name="item"> The item. </param>
    private void Set<T>(string key, T item)
    {
        HttpContext.Current.Session[key] = item;
    }
}
于 2012-05-03T19:12:06.417 に答える
5

かなり近いように見えますが、オブジェクトを実際にセッションに保存するものは何もありません。次のようなことを試してください:

public static Example Instance
{
    get
    {
        //store the object in session if not already stored
        if (Session["example"] == null)
            Session["example"] = new Example();

        //return the object from session
        return (Example)Session["example"];
    }
}

これは基本的に、 Singleton Patternの Web フレンドリーな実装です。

于 2012-05-03T19:11:45.293 に答える
5
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;

public static class ExampleSession
{
    private static HttpSessionState session { get { return HttpContext.Current.Session; } }

    public static string UserName
    {
        get { return session["username"] as string; }
        set { session["username"] = value; }
    }

    public static List<string> ProductsSelected
    {
        get
        {             
            if (session["products_selected"] == null)
                session["products_selected"] = new List<string>();

            return (List<string>)session["products_selected"];        
        }
    }
}

次のように使用できます。

public class main
{
   protected void Page_Load(object sender, EventArgs e)
   {
      //stores value into session
      ExampleSession.UserName = "foo";
      ExampleSession.ProductsSelected.Add("bar"); 
      txtInput.Text = ExampleSession.UserName; //retrieves value from session
   }
}
于 2012-05-03T19:27:23.457 に答える
2
public class example {
   public int prop1 { get; set; } 

   public static example Instance {
       var exampleObject = (example)(HttpContext.Current.Session["exampleClass"]
                                     ?? new example());

       HttpContext.Current.Session["exampleClass"] = exampleObject;

       return exampleObject; 
   } 

}

必要に応じてさらに最適化できます

于 2012-05-03T19:17:06.067 に答える
1

ここでセッションを行うためのよりオブジェクト指向の方法を探している場合は、以下でそれを行うための良い方法です。

UserSessionクラス

[Serializable()]
public class UserSession
{

    private CurrentRecord _CurrentRecord;
    public CurrentRecord CurrentRecord
    {
        get
        {
            if ((_CurrentRecord == null))
            {
                _CurrentRecord = new CurrentRecord();
            }
            return _CurrentRecord;
        }
        set
        {
            if ((_CurrentRecord == null))
            {
                _CurrentRecord = new CurrentRecord();
            }
            _CurrentRecord = value;
        }
    }
}

グローバルクラス

public static class Globals
{
    public static UserSession TheUserSession
    {
        get
        {
            if ((HttpContext.Current.Session["UserSession"] == null))
            {
                HttpContext.Current.Session.Add("UserSession", new CurrentUserSession());
                return (CurrentUserSession)HttpContext.Current.Session["UserSession"];
            }
            else
            {
                return (CurrentUserSession)HttpContext.Current.Session["UserSession"];
            }
        }
        set { HttpContext.Current.Session["UserSession"] = value; }
    }
}

CurrentRecordクラス

[Serializable()]
public class CurrentRecord
{
    public int id { get; set; }
    public string name { get; set; }
}

コードビハインドでの使用

    public void SetRecordId(int newId)
    {
        Globals.TheUserSession.CurrentRecord.id = newId;
    }
于 2012-05-03T19:48:27.703 に答える