我正在尝试将变量名作为字符串返回。因此,如果变量是var1,则我想返回字符串"var1"。是否有任何方法可以实现这一点?我听说Reflection可能是正确的方向。
编辑:本质上,我正在尝试使有组织的树形视图的实现更简单。我有一个方法,您可以在其中提供两个字符串:rootName和subNodeText。rootName恰好是变量的名称。对该方法的调用来自于针对此变量的with块内。我希望用户能够调用Method(.getVariableAsString, subNodeText)而不是Method("Variable", subNodeText)。希望以编程方式获取它的原因是这段代码可以简单地复制并粘贴。我不想每次变量被命名为异常名称时都要进行调整。
编辑:本质上,我正在尝试使有组织的树形视图的实现更简单。我有一个方法,您可以在其中提供两个字符串:rootName和subNodeText。rootName恰好是变量的名称。对该方法的调用来自于针对此变量的with块内。我希望用户能够调用Method(.getVariableAsString, subNodeText)而不是Method("Variable", subNodeText)。希望以编程方式获取它的原因是这段代码可以简单地复制并粘贴。我不想每次变量被命名为异常名称时都要进行调整。
Function aFunction()
Dim variable as Object '<- This isn't always "variable".
Dim someText as String = "Contents of the node"
With variable '<- Isn't always "variable". Could be "var", "v", "nonsense", etc
'I want to call this
Method(.GetName, someText)
'Not this
Method("Variable",someText)
End With
End Function