获取VB元素的属性

时间:2012-09-24 17:27:56

标签: properties vbscript element

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

4 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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

答案 2 :(得分:0)

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

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

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

答案 3 :(得分:0)

很容易 - 使用伪反射:

   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