我有一个带有类名称的对象,例如 MyObjName.MyClassName和MyClassName包含一个名为CalculateSum的方法,该方法接受两个参数(字符串a,布尔值b)
通过代码调用该对象非常容易,但是我需要通过将其作为字符串传递来调用
即如何设置此对象:
Dim fullObjName As String = "MyObjName.MyClassName"
Dim methodName As String = "CalculateSUM"
Dim param1 As String = "a"
Dim param2 As Boolean = True
我尝试过:
Public Shared Function InvokeStringMethod2(ByVal typeName As String, ByVal methodName As String, ByVal stringParam As String, boolparam As Boolean) As String
Dim calledType As Type = Type.[GetType](typeName)
Dim s As String = CType(calledType.InvokeMember(methodName, BindingFlags.InvokeMethod Or BindingFlags.[Public] Or BindingFlags.[Static], Nothing, Nothing, New Object() {stringParam, boolparam}), String)
Return s
End Function
答案 0 :(得分:0)
戴夫(Dave)的评论使我的工作
Dim obj As New Object
obj = CreateObject("MyObjName.MyClassName")
Microsoft.VisualBasic.CallByName(obj, "CalculateSUM", CallType.Method, "a", True)
如果需要在同一对象中调用另一个方法,则可以使用
Microsoft.VisualBasic.CallByName(obj, "Show_Form", CallType.Method)