Sometimes you need to execute code in a separate class, say from a windows form. This piece of code does just that by using DirectCast to invoke the Function ‘MyFunctionName’ from the class ‘MyClassName’.  The function, in this instance, returns a boolean.

Dim cls As Object = "MyClassName"
cls = CType(cls, Object)
Dim calledType As Type = Type.[GetType](cls)
Dim ok as Boolean = DirectCast(calledType.InvokeMember("MyFunctionName", BindingFlags.InvokeMethod Or BindingFlags.[Public] Or BindingFlags.[Static], Nothing, Nothing, Nothing), [Boolean])

Leave a reply

Your email address will not be published.