1

我有很多类,每个类都有 15 个属性(至少),我想构建一个函数来返回一个包含所有属性的字符串。简单的方法是在每个类中添加一个 this 函数:

Public function getAllAttributes(instance as Object) as String
 Dim str as String
 str = str & “**”& instance. Attribute1 & .... &“**”&  instance. Attribute100
 getAllAttributes = str
End function

但我想构建一个适用于所有类的函数(伪代码中的想法是:)

Public function getAllAttributes(instance as Object) as String
 ‘function that handles all classes
 Dim str as String
 For att in instance 
    Str = str & “**”&  att.value
 Next
 getAllAttributes = str
End function

我使用了 typelib 信息参考,但我只能获得属性名称。

谢谢。

4

1 回答 1

3

我找到了。

添加 TypeLib 信息作为对项目的引用。

然后,使用这两个函数:

Public Function CharExecution(pObject As Object) As String
    CharExecution = “”
    Dim TLI         As TLIApplication
    Dim lInterface  As InterfaceInfo
    Dim lMember     As MemberInfo

    Set TLI = New TLIApplication
    Set lInterface = TLI.InterfaceInfoFromObject(pObject)

    Set ClassInfo = TLI.InterfaceInfoFromObject(pObject)
    Set FilteredMembers = ClassInfo.Members.GetFilteredMembers

    For Each lMember In lInterface.Members
        If WhatIsIt(lMember) = "Property Get" Then
            CharExecution = CharExecution & "*****" & lMember.Name & " : " & TLI.InvokeHook(pObject, lMember.Name, INVOKE_PROPERTYGET)
        End If
    Next
    Set pObject = Nothing
    Set lInterface = Nothing
    Set TLI = Nothing        
  End Function

   '================================================================================

 Private Function WhatIsIt(lMember As Object) As String
  Select Case lMember.InvokeKind
    Case INVOKE_FUNC
        If lMember.ReturnType.VarType <> VT_VOID Then
            WhatIsIt = "Function"
        Else
            WhatIsIt = "Method"
        End If
    Case INVOKE_PROPERTYGET
        WhatIsIt = "Property Get"
    Case INVOKE_PROPERTYPUT
        WhatIsIt = "Property Let"
    Case INVOKE_PROPERTYPUTREF
        WhatIsIt = "Property Set"
    Case INVOKE_CONST
        WhatIsIt = "Const"
    Case INVOKE_EVENTFUNC
        WhatIsIt = "Event"
    Case Else
        WhatIsIt = lMember.InvokeKind & " (Unknown)"
 End Select
End Function

称它为:

  MsgBox CharExecution( classInstanceName)

或者

  MsgBox CharExecution(New ClassName)

谢谢 。

于 2013-10-26T08:08:29.617 回答