我正在寻找一种检索变量名称的方法,因此我不必在需要时使用硬编码声明(对于属性名称等):
我几乎不相信这是可能的。也许有人有解决方案。注意:即使不是变量,属性也会是一个动作。
'Pseudo:
Module Module1
Sub Main()
Dim variable = "asdf"
Dim contact As New Contact
Dim v1 = GetVariableName(variable) 'returns variable
Dim v2 = GetVariableName(contact.Name) 'returns Name
End Sub
Class Contact
Public ReadOnly Property Name()
Get
Return Nothing
End Get
End Property
End Class
Public Function GetVariableName(variable As Object) As String
':}
End Function
End Module
欢迎使用 VB 或 C# 来回答。