Invokes the method reflected by this MethodInfo instance. Invokes the underlying method represented by this Method object, on the specified object with the specified parameters.
When implemented by a subclass, this method invokes the reflected method with the given parameters.
[Visual Basic] Overloads MustOverride Public Function Invoke(Object, BindingFlags, Binder, Object(), CultureInfo) As Object
[C#] public abstract object Invoke(Object, BindingFlags, Binder, Object[], CultureInfo);
[C++] public: virtual Object* Invoke(Object*, BindingFlags, Binder, Object*[], CultureInfo) = 0;
[JScript] public abstract function Invoke(Object, BindingFlags, Binder, Object[], CultureInfo) : Object;
Invokes the method reflected by this instance using the given parameters
[Visual Basic] Overloads Public Function Invoke(Object, Object()) As Object
[C#] public object Invoke(Object, Object[]);
[C++] public: Object* Invoke(Object*, Object*[]);
[JScript] public function Invoke(Object, Object[]) : Object;
The following Managed C++ sample demonstrates dynamic method lookup via reflection. Note that all of the invocations return 1, because the method in classes A and B is virtual.
Note This example shows how to use one of the overloaded versions of Invoke. For other examples that may be available, see the individual overload topics.
[C++] #import <mscorlib.dll> using namespace Reflection; #define NULL 0 __managed class A { public: virtual int method() { return 0;} }; __managed class B : public A { public: virtual int method() { return 1;} }; void main () { A *objA = new A(); B *objB = new B(); Type *typeA = objA->GetType(); Type *typeB = objB->GetType(); MethodInfo *methodA = typeA->GetMethod(L"method"); MethodInfo *methodB = typeB->GetMethod(L"method"); Console::WriteLine(L"\tUsing reflection to invoke A::method on objB returns {0}.", dynamic_cast<Object *>(methodA->Invoke(objB, NULL))); Console::WriteLine(L"\tUsing Invoke on objB after converting to type A returns {0}.", dynamic_cast<Object *>(methodA->Invoke(dynamic_cast<A *>(objB), NULL))); Console::WriteLine(L"\r\nThe same behavior is seen when the method "); Console::WriteLine(L"is invoked without reflection:\r\n"); Console::WriteLine(L"\tDirectly invoking B::method on objB returns {0}.", Int32::Box(objB->method()) ); Console::WriteLine(L"\tDirectly invoking B::method on objB after converting to type A returns {0}.", Int32::Box(dynamic_cast<A *>(objB)->method())); };
[C#]
public class A { public virtual int method () {return 0;} } public class B { public virtual int method () {return 1;} } class Mymethodinfo { public static int Main() { Console.WriteLine ("\nReflection.MethodInfo"); A MyA = new A(); B MyB = new B(); //Get the Type and MethodInfo Type MyTypea = Type.GetType("A"); MethodInfo Mymethodinfoa = MyTypea.GetMethod("method"); Type MyTypeb = Type.GetType("B"); MethodInfo Mymethodinfob = MyTypeb.GetMethod("method"); //Get and display the Invoke method Console.Write("\nFirst method - " + MyTypea.FullName + " returns " + Mymethodinfoa.Invoke(MyA, null)); Console.Write("\nSecond method - " + MyTypeb.FullName + " returns " + Mymethodinfob.Invoke(MyB, null)); return 0; } }
Produces the following output
MethodInfo Class | MethodInfo Members | System.Reflection Namespace