会话.net中的通用字典



我有一个称为SessionManager的通用类来控制我在会话中存储的变量类型。我一直在为String, Integer, List(Of T)做这件事,没有任何这样的问题。

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)做同样的事情。我想为Dictionary(Of T, T)提供2个通用私有方法,用于获取和设置;GetFromSessionAsDictionary和setsessionasdictionary。(当没有像List(Of T)属性和set在会话中返回空字典时)

这应该允许我创建尽可能多的公共属性类型字典(例如字典(String, String),字典(String, Integer))尽可能存储在会话。有可能做到吗?如果有,怎么做?我已经试过了,但是它把我弄糊涂了。

编辑

这就是我在试图使它成为通用字典(Of T, T)后所做的事情,它的工作没有任何问题。我想要的是一种解决方案,它不会将类型约束为Dictionary(Of String, Integer)。

Private Const _dictionary1_ofstringint As 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

编辑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

谢谢你的建议!

自己找到的解决方案,抱歉浪费您的时间,谢谢!

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

最新更新