我有一個名爲SessionManager的泛型類來控制會話中存儲的變量類型。我一直在做這樣的String,Integer,List(Of T),沒有這樣的問題。會話中的泛型字典.Net
Public NotInheritable Class SessionManager
'''<remarks>
'''Private constructor to prevent instantiation of, or inheritance from, this class.
'''</remarks>
Private Sub New()
End Sub
Private Const _PropertyOfSomeClass As String = "PROPERY_OF_SOME_CLASS"
Private Const _ListOfSomeClass As String = "LIST_OF_SOME_CLASS"
Public Shared Property PropertyOfSomeClass() As SomeClass
Get
Return GetFromSession(Of SomeClass)(_PropertyOfSomeClass)
End Get
Set(value As SomeClass)
SetInSession(Of SomeClass)(_PropertyOfSomeClass, value)
End Set
End Property
Public Shared Property ListOfSomeClass() As List(Of SomeClass)
Get
Return GetFromSessionAsList(Of SomeClass)(_ListOfSomeClass)
End Get
Set(value As List(Of SomeClass))
SetInSessionAsList(Of SomeClass)(_ListOfSomeClass, value)
End Set
End Property
Private Shared Function GetFromSession(Of T)(key As String) As T
Dim obj As Object = HttpContext.Current.Session(key)
If obj Is Nothing Then
Return Nothing
End If
Return DirectCast(obj, T)
End Function
Private Shared Function GetFromSessionAsList(Of T)(key As String) As List(Of T)
Dim obj As Object = HttpContext.Current.Session(key)
If obj Is Nothing Then
SetInSessionAsList(Of T)(key, New List(Of T))
Return New List(Of T)
End If
Return DirectCast(obj, List(Of T))
End Function
Private Shared Sub SetInSession(Of T)(key As String, value As T)
If value Is Nothing Then
HttpContext.Current.Session.Remove(key)
Else
HttpContext.Current.Session(key) = value
End If
End Sub
Private Shared Sub SetInSessionAsList(Of T)(key As String, value As List(Of T))
If value Is Nothing Then
HttpContext.Current.Session(key) = New List(Of T)
Else
HttpContext.Current.Session(key) = value
End If
End Sub
End Class
我想對Dictionary(Of T,T)做同樣的事情。我想要2個通用的私有方法來獲取和設置Dictionary(Of T,T) GetFromSessionAsDictionary和SetInSessionAsDictionary。 (當沒有任何東西返回像List(Of T)屬性的空字典並在會話中設置時)
這應該允許我創建類型字典的許多公共屬性(例如字典(字符串,字符串),字典字符串,整數))儘可能存儲在會話中。有可能嗎?如果是這樣,怎麼樣?我已經去過了,但它讓我感到困惑。
編輯
這是我已經結束了試圖使其通用詞典(T,T)之後做的,它的工作沒有任何問題。我想要的是一種不會將類型限制爲字典(字符串,整數)的解決方案。
私人常量_Dictionary1OfStringInt的String = 「DICTIONARY_1_OF_STRING_INT」
Public Shared Property DictionaryOf As Dictionary(Of String, Integer)
Get
Return GetFromSessionAsDictionary(_Dictionary1OfStringInt)
End Get
Set(value As Dictionary(Of String, Integer))
SetInSessionAsDictionary(_Dictionary1OfStringInt, value)
End Set
End Property
Private Shared Function GetFromSessionAsDictionary(key As String) As Dictionary(Of String, Integer)
Dim obj As Object = HttpContext.Current.Session(key)
If obj Is Nothing Then
SetInSessionAsDictionary(key, New Dictionary(Of String, Integer))
Return New Dictionary(Of String, Integer)
End If
Return DirectCast(obj, Dictionary(Of String, Integer))
End Function
Private Shared Sub SetInSessionAsDictionary(key As String, value As Dictionary(Of String, Integer))
If value Is Nothing Then
HttpContext.Current.Session(key) = New Dictionary(Of String, Integer)
Else
HttpContext.Current.Session(key) = value
End If
End Sub
EDIT 2
嗯,我做到了我自己。我對傳遞TKey,TValue感到困惑,因爲我認爲T只能傳遞一次。
Private Shared Function GetFromSessionAsDictionary(Of TKey, TValue)(key As String) As Dictionary(Of TKey, TValue)
Dim obj As Object = HttpContext.Current.Session(key)
If obj Is Nothing Then
SetInSessionAsDictionary(key, New Dictionary(Of TKey, TValue))
Return New Dictionary(Of TKey, TValue)
End If
Return DirectCast(obj, Dictionary(Of TKey, TValue))
End Function
Private Shared Sub SetInSessionAsDictionary(Of TKey, TValue)(key As String, value As Dictionary(Of TKey, TValue))
HttpContext.Current.Session(key) = If(value, New Dictionary(Of TKey, TValue)())
End Sub
感謝您的建議!
什麼讓你感到困惑?你在哪裏遇到困難? – Oded
@Hiral你可以展示你爲Dictonary(Of T,T)所做的嘗試嗎?這將有助於 – msarchet
也只是有趣的注意你可以做一些像'HttpContext.Current.Session(key)= If(value,New List(Of T)'而不是無處檢查 – msarchet