主题:[讨论]VB里也有反射机制(CallByName)?
Option Explicit
Private Sub Command1_Click()
'直接用函数名进行调用,省去了if或select case等判断
CallByName Me, Combo1.Text, VbMethod
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 4
Combo1.AddItem "f" & i
Next
Combo1.ListIndex = 0
End Sub
Public Function f1() As Long
Debug.Print "this is f1()"
End Function
Public Function f2() As Long
Debug.Print "this is f2()"
End Function
Public Function f3() As Long
Debug.Print "this is f3()"
End Function
Public Function f4() As Long
Debug.Print "this is f4()"
End Function
哈哈,这就是所谓的反射?。不过只能算是精简版,还是得人为地知道一个对象有哪些方法,属性,没有getMethods,getProperties.不能在运行时动态地取得。
Private Sub Command1_Click()
'直接用函数名进行调用,省去了if或select case等判断
CallByName Me, Combo1.Text, VbMethod
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 4
Combo1.AddItem "f" & i
Next
Combo1.ListIndex = 0
End Sub
Public Function f1() As Long
Debug.Print "this is f1()"
End Function
Public Function f2() As Long
Debug.Print "this is f2()"
End Function
Public Function f3() As Long
Debug.Print "this is f3()"
End Function
Public Function f4() As Long
Debug.Print "this is f4()"
End Function
哈哈,这就是所谓的反射?。不过只能算是精简版,还是得人为地知道一个对象有哪些方法,属性,没有getMethods,getProperties.不能在运行时动态地取得。