5

如何查看元素在 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 中,我可以从该工具中读取(用户定义的)属性。但是在运行脚本时,我收到一个错误,告诉我它不知道该用户定义的属性的名称。但我在工具中使用它。现在我想知道上面的数组中有哪些属性可用,以查看用户定义的属性是否被具体命名。

4

4 回答 4

2

真的不可能。VBScript 运行时中只有非常基本的类型信息可用。理想情况下,您可以创建一个适配器,将您的工具对象转换为标准 Dictionary 对象并迭代 Keys。如果这不可能,您能做的最好的事情是在调用其成员之前检查每个对象的类型名称。例子:

<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>
于 2012-10-06T02:20:18.273 回答
1
Dim list : Set list = CreateObject( "Scripting.Dictionary" )
' ... Fill List ...
WriteListElements list
...

Sub WriteListElements ( list )
    Dim e, le     
    For Each e In list
        Set le = e.Items                
        Response.Write le(name_of_user_defined_attribut)
    Next
End Sub
于 2012-10-06T09:21:59.947 回答
0

你能从这样的脚本中获取属性列表吗?

http://www.vbsedit.com/scripts/misc/wmi/scr_1332.asp

然后,您可以使用 eval() 或执行来获取这些属性的值。

于 2012-10-02T15:18:49.317 回答
0

It is easy - use a pseudo reflection:

   class Developer

      Public reflection
     '=============================
     'Private properties
      private mId

      private  mFirstName
      private  mLastName

      private sub Class_Initialize()
        reflection = Array("Id","FirstName","LastName")
      end sub

      private sub Class_Terminate()
      end sub

     '=============================
     'public properties

       public property get Id()
          Id = mId
       end property

       public property let Id(val)
          mId = val
       end property


       public property get FirstName()
          FirstName = mFirstName
       end property  

       public property let FirstName(val)
          mFirstName = val
       end property  

       public property get LastName()
          LastName = mLastName
       end property  

       public property let LastName(val)
          mLastName = val
       end property  

    end class


    For each property in obj.reflection 
     document.write(property)
     document.write( Eval ("obj." & property) )
    Next 
于 2013-05-02T04:35:53.473 回答