字典怎么样?
<Serializable()>
Public Class UserData
Public Property UserGUID As GUID
Public Property Parameters As Dictionary(Of String, String)
End Class
然后你可以让字典充满如下内容:
{{"Name", "Sir Launcelot of Camelot"},
{"Quest","To seek the Holy Grail"},
{"Favorite Color","Blue"},
{"Favorite Place","Castle Anthrax"},
...
}
你可以有一个如下所示的表格:
UserGUID | Parameter | Value
-----------------------------------------------------------
AZ-12-43-EF | Name | Sir Launcelot of Camelot
AZ-12-43-EF | Quest | To seek the Holy Grail
AZ-12-43-EF | Favorite Color | Blue
34-DF-E4-22 | Name | Sir Galahad of Camelot
34-DF-E4-22 | Quest | I seek the Holy Grail
34-DF-E4-22 | Favorite Color | Blue. No yel-- Auuuuuuuugh!
如果您需要更复杂的参数值,也许您可以存储一个与每个参数一起使用的 JSON 字符串? ...或者,如果您已经走到了那一步,您可能会将所有用户数据设为 JSON 字符串;)
编辑:
要显示数据,您有多种选择,如果您知道想要什么条目,您可以简单地从字典中抓取它:
If userData.Parameters.ContainsKey("Name") Then
textBoxName = userData.Parameters("Name")
Else
'Name value not found
textBoxName = "Enter name here"
End If
如果您想在表格中显示所有参数,则必须先将字典转换为其他内容。例如,要绑定到DataGridView,您可以执行以下操作:
Dim parameterList = From entry In userData.Parameters
Select New With { .Parameter = entry.Key, entry.Value }
dataGridView1.DataSource = parameterList.ToArray()
在上面的示例中,parameterList 实际上是 anonymous type 的 IEnumerable,具有两个属性,Parameter As String 和 Value As String。
了解更多关于Dictionary的资源:
...以及一些 StackOverflow 问题: