如何使用用户指定的名称声明字典?
How to declare a dictionary using a name the user specifies?
显然我不能在 Dictionary 声明行中使用 strName,但我只是将它放在那里以表示我正在尝试做的事情。例如,如果用户输入“carrot”,我希望创建的字典被命名为carrot。有办法吗?
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strName As String
strName = TextBox1.Text
Dim strName As New Dictionary(Of String, String)
End Sub
End Class
你不能。该名称对于调试很有用,实际上编译器无论如何都不会使用该名称 - 该名称将保存在 pdb 文件中,别无他处。
如果您需要按名称跟踪某些词典,您可以使用其他词典,例如:
Private dictionaries As New Dictionary(Of String, Dictionary(Of String, String))()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strName = TextBox1.Text
Dim myNamedDictionary As Dictionary(Of String, String)
If dictionaries.ContainsKey(strName) Then
myNamedDictionary = dictionaries(strName)
Else
myNamedDictionary = New Dictionary(Of String, String)()
dictionaries.Add(strName, myNamedDictionary)
End If
' now you have a dictionary for the name you entered (carrot)
End Sub
检索
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim carrotDictionary = dictionaries("carrot")
End Sub
我想这已经很接近了。
你可以,有点。使用编译器你可以编译自己的代码,但最终结果似乎无法使用,我怀疑这是你想要做的
Option Strict Off
Imports System.Reflection
Imports System.CodeDom.Compiler
Public Class Form1
Private dictionaryInstance As Object
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strName = TextBox1.Text
Dim t = GetType(Form1)
Dim ns = t.Namespace
Dim provider = New VBCodeProvider()
Dim params = New CompilerParameters()
params.GenerateInMemory = True
params.ReferencedAssemblies.Add(Assembly.GetEntryAssembly().Location)
params.OutputAssembly = "OutputAssembly"
Dim code =
$"
Imports System
Imports System.Collections.Generic
Namespace {ns}
Partial Public Class DictionaryClass
Public {strName} As New Dictionary(Of String, String)
End Class
End Namespace"
Dim results = provider.CompileAssemblyFromSource(params, {code})
Dim assy = results.CompiledAssembly
Dim o As Object = assy.CreateInstance($"{ns}.DictionaryClass")
Me.dictionaryInstance = o
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
dictionaryInstance.carrot.Add("a", "b")
End Sub
End Class
显然我不能在 Dictionary 声明行中使用 strName,但我只是将它放在那里以表示我正在尝试做的事情。例如,如果用户输入“carrot”,我希望创建的字典被命名为carrot。有办法吗?
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strName As String
strName = TextBox1.Text
Dim strName As New Dictionary(Of String, String)
End Sub
End Class
你不能。该名称对于调试很有用,实际上编译器无论如何都不会使用该名称 - 该名称将保存在 pdb 文件中,别无他处。
如果您需要按名称跟踪某些词典,您可以使用其他词典,例如:
Private dictionaries As New Dictionary(Of String, Dictionary(Of String, String))()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strName = TextBox1.Text
Dim myNamedDictionary As Dictionary(Of String, String)
If dictionaries.ContainsKey(strName) Then
myNamedDictionary = dictionaries(strName)
Else
myNamedDictionary = New Dictionary(Of String, String)()
dictionaries.Add(strName, myNamedDictionary)
End If
' now you have a dictionary for the name you entered (carrot)
End Sub
检索
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim carrotDictionary = dictionaries("carrot")
End Sub
我想这已经很接近了。
你可以,有点。使用编译器你可以编译自己的代码,但最终结果似乎无法使用,我怀疑这是你想要做的
Option Strict Off
Imports System.Reflection
Imports System.CodeDom.Compiler
Public Class Form1
Private dictionaryInstance As Object
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strName = TextBox1.Text
Dim t = GetType(Form1)
Dim ns = t.Namespace
Dim provider = New VBCodeProvider()
Dim params = New CompilerParameters()
params.GenerateInMemory = True
params.ReferencedAssemblies.Add(Assembly.GetEntryAssembly().Location)
params.OutputAssembly = "OutputAssembly"
Dim code =
$"
Imports System
Imports System.Collections.Generic
Namespace {ns}
Partial Public Class DictionaryClass
Public {strName} As New Dictionary(Of String, String)
End Class
End Namespace"
Dim results = provider.CompileAssemblyFromSource(params, {code})
Dim assy = results.CompiledAssembly
Dim o As Object = assy.CreateInstance($"{ns}.DictionaryClass")
Me.dictionaryInstance = o
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
dictionaryInstance.carrot.Add("a", "b")
End Sub
End Class