私の主な問題は、ソースを読みにくくするセッション変数を設定/取得するメソッド呼び出しがいっぱいであるというコードがあることです。私はより良い/よりシンプルな/よりエレガントなソリューションを探しています。私はクラス、ラッパークラス、暗黙の型変換で演算子のオーバーロードを試みましたが、私はそれらすべてに問題があります。通常の変数のようなセッション変数を扱う
通常の変数のようなセッション変数を扱いたいと思います。 多くの記事を読んだ後、私はさらに簡単したいのですが、次の解決策を考え出した:だから
public static SV_string UserID = new SV_string("UserID");
UserID.val = "Admin"; //Now the value assignment is quite simple
string user = UserID.val; //Getting the data is quite simple too
UserID = "Admin"; //but it would be even simpler
:
public class SV_string
{
private string key = ""; //to hold the session variable key
public SV_string(string key)
{
this.key = key; // I set the key through the constructor
}
public string val // I use this to avoid using setter/getter functions
{
get
{
return (string)System.Web.HttpContext.Current.Session[key];
}
set
{
System.Web.HttpContext.Current.Session[key] = value;
}
}
}
私は変数名と同じキーを使用します希望の動作を得るための方法はありますか?
ありがとうございます!
これを行う方法はないと思うし、C#デベロッパーにとって直感的ではないので、これをやりたいと思うべきかどうかはわかりません。代わりにメソッドを使うほうが良いでしょう( 'UserID.SetValue(" Admin ")')。 –
これは単なるファサードのデザインパターンです。私にとって完璧にうまくいくと思う。 – Enigmativity
あなたの命名はひどいです。BTW –