Question:You write the following code to implement the MyClass.MyMethod function.
Public Class NewClass
Public Function MyMethod(ByVal Arg As Integer) As Integer
Return Arg
End Function
End Class
You need to call the MyClass.MyMethod function dynamically from an unrelated class in your assembly.
Which code segment should you use?
 

A Dim objNewClass As New NewClass
Dim objType As Type = objNewClass.GetType
Dim objInfo As MethodInfo = _
objType.GetMethod("MyMethod")
Dim objParams() As Object = {1}
Dim i As Integer = _
DirectCast(objInfo.Invoke(Me, objParams), Integer) 

B Dim objNewClass As New NewClass
Dim objType As Type = objNewClass.GetType
Dim objInfo As MethodInfo = objType.GetMethod("MyMethod")
Dim objParams() As Object = {1}
Dim i As Integer = _
DirectCast(objInfo.Invoke(objNewClass, objParams), Integer) 

C Dim objNewClass As New NewClass
Dim objType As Type = objNewClass.GetType
Dim objInfo As MethodInfo = _
objType.GetMethod("NewClass.MyMethod")
Dim objParams() As Object = {1}
Dim i As Integer = _
DirectCast(objInfo.Invoke(objNewClass, objParams), Integer) 

D Dim objType As Type = Type.GetType("NewClass")
Dim objInfo As MethodInfo = objType.GetMethod("MyMethod")
Dim objParams() As Object = {1} Dim i As Integer = _
DirectCast(objInfo.Invoke(Me, objParams), Integer) 

+ Answer
+ Report
Total Preview: 834

Copyright © 2024. Powered by Intellect Software Ltd