5

How can I see what properties an element has in a VB script? Example:

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

I use a Tool with a VBScript API. In that API I can read (user defined) attributes from that Tool. But while running the script I get an error telling me that it does not know the name of that user defined attribut. But I use it in the tool. Now I would like to know which attributes are availble in the array above to see if the user defined attributes are named specificly.

4

4 に答える 4

2

本当にありえない。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>
于 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

簡単です - 疑似反射を使用します:

   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 に答える