2010-06-14 8 views

Odpowiedz

12
List<string> ast = new List<string>(); 
     ast.Add("asdas!"); 
     Session["stringList"] = ast; 
     List<string> bst = (List<string>)Session["stringList"]; 
+0

to działa z więcej niż 1 wartością listy? – JoJo

3

Możesz robić tego rodzaju rzeczy, jeśli o to pytasz.

Session["key"] = List<string>; 

jak

myStrings = (List<string>)Session["key"]; 
+1

Sesja ["klucz"] jako lista jest lepsza. I pamiętaj, aby sprawdzić, czy przed konwersją jest wartość null. –

2

Możesz zbadać następującą metodę rozszerzenia dla HttpSessionState dwie klasy.

public static System.Nullable<T> GetValue<T>(this HttpSessionState session, string key) where T : struct, IConvertible 
    { 
     object value = session[key]; 
     if (value != null && value is T) 
     { 
      return (T)value; 
     } 
     else 
      return null; 
    } 


    public static T GetValue<T>(this HttpSessionState session, string key, T defaultValue) where T : class 
    { 
     object value = session[key] ?? defaultValue; 
     if (value != null && value is T) 
     { 
      return (T)value; 
     } 
     else 
      return default(T); 
    } 

Pierwszy typ wartości, a drugi typ odniesienia.

Użycie jest w następujący sposób:

int? _customerId = Session.GetValue<int>("CustomerID"); 
    Customer _customer = Session.GetValue<Customer>("CurrentCustomer", null); 
0

Tak.

var myList=(List<String>)Session["toList"]; 
Powiązane problemy