2

有没有办法my.settings动态创建变量?我想根据数据库中的值生成 my.settings 变量,因此 my.settings 变量的数量需要不时变化。

如果我的数据库中有以下值,

0
2
6
13
77

生成的 my.settings 变量的名称将是

My.Settings.M0
My.Settings.M2
My.Settings.M6
My.Settings.M13
My.Settings.M77

所以我想在应用程序第一次运行时创建这些变量。从数据库中获取数字后的问题。如何使用代码创建这些变量?

还有一种方法可以用代码删除它们因为当数据库的值发生变化并且值不存在时,我需要删除它的变量?

我也想要一些建议,如果这不是一个好方法。

4

1 回答 1

3

项目设置似乎在序列化和保存基本类型之外的值方面做得不好。您可以做的是使用用户范围的字符串值设置来存储序列化字典。

对于我的示例,我创建了一个名为SerializedKeyPercentDictionaryTypestring和 Scope的设置User。我使用 JSON 进行序列化,因为它创建的字符串比大多数其他序列化的长度更短。为此,您需要添加对 System.Runtime.Serializations 的引用。有了这个设置和那个引用,你就可以创建一个全局帮助类来提供一个强类型的字典来管理你的键到百分比的映射:

Public Class KeyPercentHelper
    Private Shared _keyPercentDictionary As Dictionary(Of Integer, Decimal)
    Private Shared _initLock As Object = New Object()

    Public Shared ReadOnly Property KeyPercentDictionary As Dictionary(Of Integer, Decimal)
        Get
            If (_keyPercentDictionary Is Nothing) Then
                InitializeDictionary()
            End If
            Return _keyPercentDictionary
        End Get
    End Property

    Shared Sub New()
        AddHandler My.Settings.SettingsLoaded, AddressOf HandleSettingsLoad
        AddHandler My.Settings.SettingsSaving, AddressOf HandleSettingsSaving
    End Sub

    Private Shared Sub InitializeDictionary()
        ' Load dictionary from User Setting.
        SyncLock _initLock
            If (_keyPercentDictionary Is Nothing) Then
                If (String.IsNullOrEmpty(My.Settings.SerializedKeyPercentDictionary)) Then
                    _keyPercentDictionary = New Dictionary(Of Integer, Decimal)()
                Else
                    Dim ser As New System.Runtime.Serialization.Json.DataContractJsonSerializer(GetType(Dictionary(Of Integer, Decimal)))
                    Using memStream As New System.IO.MemoryStream()
                        Using writer As New System.IO.StreamWriter(memStream)
                            writer.Write(My.Settings.SerializedKeyPercentDictionary)
                            writer.Flush()
                            memStream.Position = 0
                            _keyPercentDictionary = CType(ser.ReadObject(memStream), Dictionary(Of Integer, Decimal))
                        End Using
                    End Using
                End If
            End If
        End SyncLock
    End Sub

    Private Shared Sub HandleSettingsLoad(ByVal sender As Object, ByVal e As EventArgs)
        If (_keyPercentDictionary Is Nothing) Then
            InitializeDictionary()
        End If
    End Sub

    Private Shared Sub HandleSettingsSaving(ByVal sender As Object, ByVal e As EventArgs)
        ' Ensure User Setting value is updated before save.
        Dim ser As New System.Runtime.Serialization.Json.DataContractJsonSerializer(GetType(Dictionary(Of Integer, Decimal)))
        Using memStream As New System.IO.MemoryStream()
            ser.WriteObject(memStream, _keyPercentDictionary)
            memStream.Position = 0
            Using reader As New System.IO.StreamReader(memStream)
                My.Settings.SerializedKeyPercentDictionary = reader.ReadToEnd()
            End Using
        End Using
    End Sub
End Class
于 2012-08-03T18:34:37.953 回答