我有很多类,每个类都有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信息引用,但我只能获取属性名称。
感谢。
答案 0 :(得分:2)
我找到了。
添加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)
谢谢。