如何在 VB 脚本中查看元素具有哪些属性?例子:
Dim list : Set list = CreateObject( "Scripting.Dictionary" )
' ... Fill List ...
WriteListElements list
...
Sub WriteListElements ( list )
Dim e, le
For Each e In list
Set le = list(e) ' what properties does le have?
le.name_of_user_defined_attribut ' I want to access a property but dont know the exact name
Next
End Sub
我使用带有 VBScript API 的工具。在该 API 中,我可以从该工具读取(用户定义的)属性。但是在运行脚本时,我收到一个错误,告诉我它不知道该用户定义的属性的名称。但我在工具中使用它。现在我想知道上面的数组中有哪些属性可用,以查看用户定义的属性是否被具体命名。
最佳答案
不太可能。 VBScript 运行时中只有非常基本的类型信息可用。理想情况下,您可以创建一个适配器,将您的工具对象转换为标准 Dictionary 对象并迭代键。如果这是不可能的,您能做的最好的事情就是在调用其成员之前检查每个对象的类型名称。例子:
<html>
<body>
<script type="text/vbscript">
Class Human
Private m_name
Public Property Get Name
Name = m_name
End Property
Public Property Let Name(newName)
m_name = newName
End Property
End Class
Dim joe
Set joe = new Human
joe.Name = "Joe Coder"
Dim list
Set list = CreateObject( "Scripting.Dictionary" )
list.Add "a", 5
list.Add "b", joe
list.Add "c", "apples"
WriteListElements list
Sub WriteListElements ( list )
Dim e
For Each e In list
If (TypeName(list.Item(e)) = "Human") Then
document.write("We have found a Human: " &_
"<b>" & list.Item(e).Name & "</b>")
End If
Next
End Sub
</script>
</body>
</html>
关于properties - 获取VB元素的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12569931/