如何在 VB.Net 中声明一个包含字符串键和命名元组作为值的字典?
How do I declare a Dictionary in VB.Net with a string key and a named Tuple as the value?
我正在声明一个数据结构并用数据加载它
Public Shared LexerEnglishFullWithTuple As New Dictionary(Of String, Object) From {
{"LET", (1, "000000001", 0)},
{"PUT", (2, "000000002", 2)},
...
我希望能够做的是命名 ValueTuple 的组件,并且在一定程度上这可行,即,
Public Shared LexerEnglishFullWithTuple As New Dictionary(Of String, Object) From {
{"LET", (Id:=1, IdString:="000000001", Arity:=0)},
{"PUT", (Id:=2, IdString:="000000002", Arity:=2)},
...
我目前坚持的是 As New Dictionary(Of String, Object)
能够放置 Object 以外的东西会很好。我已经尝试过 Tuple
、ValueTuple
和各种带有 (Of
的东西,但似乎没有任何东西可以编译。
在我的测试中,因为只是Of String,Object
我看不到Id,IdString和Arity,所以我只能看到Item1,Item2和Item3
<Fact>
Sub TestSub2()
Dim FEL = Lexers.English.LexerEnglishFullWithTuple
Dim x = FEL("EDS")
Dim y = From el In FEL Where el.Value.Item1 = 861220001 Select el.Key
Assert.Equal("EDS", y(0))
End Sub
您只需像声明其他任何内容一样声明元组属性的名称和类型:
Public Shared LexerEnglishFullWithTuple As New Dictionary(Of String, (Id As Integer, IdString As String, Arity As Integer)) From {
{"LET", (1, "000000001", 0)},
{"PUT", (2, "000000002", 2)},
'...
试试这个:
Dim LexerEnglishFullWithTuple As New Dictionary(Of String, (Id As Integer, IdString As String, Arity As Integer)) From {
{"LET", (1, "000000001", 0)},
{"PUT", (2, "000000002", 2)}
}
For Each x In LexerEnglishFullWithTuple
Dim value = x.Value
Console.WriteLine(value.Id)
Console.WriteLine(value.IdString)
Console.WriteLine(value.Arity)
Next
我正在声明一个数据结构并用数据加载它
Public Shared LexerEnglishFullWithTuple As New Dictionary(Of String, Object) From {
{"LET", (1, "000000001", 0)},
{"PUT", (2, "000000002", 2)},
...
我希望能够做的是命名 ValueTuple 的组件,并且在一定程度上这可行,即,
Public Shared LexerEnglishFullWithTuple As New Dictionary(Of String, Object) From {
{"LET", (Id:=1, IdString:="000000001", Arity:=0)},
{"PUT", (Id:=2, IdString:="000000002", Arity:=2)},
...
我目前坚持的是 As New Dictionary(Of String, Object)
能够放置 Object 以外的东西会很好。我已经尝试过 Tuple
、ValueTuple
和各种带有 (Of
的东西,但似乎没有任何东西可以编译。
在我的测试中,因为只是Of String,Object
我看不到Id,IdString和Arity,所以我只能看到Item1,Item2和Item3
<Fact>
Sub TestSub2()
Dim FEL = Lexers.English.LexerEnglishFullWithTuple
Dim x = FEL("EDS")
Dim y = From el In FEL Where el.Value.Item1 = 861220001 Select el.Key
Assert.Equal("EDS", y(0))
End Sub
您只需像声明其他任何内容一样声明元组属性的名称和类型:
Public Shared LexerEnglishFullWithTuple As New Dictionary(Of String, (Id As Integer, IdString As String, Arity As Integer)) From {
{"LET", (1, "000000001", 0)},
{"PUT", (2, "000000002", 2)},
'...
试试这个:
Dim LexerEnglishFullWithTuple As New Dictionary(Of String, (Id As Integer, IdString As String, Arity As Integer)) From {
{"LET", (1, "000000001", 0)},
{"PUT", (2, "000000002", 2)}
}
For Each x In LexerEnglishFullWithTuple
Dim value = x.Value
Console.WriteLine(value.Id)
Console.WriteLine(value.IdString)
Console.WriteLine(value.Arity)
Next