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

プロジェクト設定は、基本タイプ以外の値のシリアル化と保存ではうまく機能しないようです。できることは、ユーザースコープの文字列値設定を使用して、シリアル化されたディクショナリを格納することです。

私の例ではSerializedKeyPercentDictionary、Typestringと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 に答える