NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

MethodInfo.Invoke (Object, BindingFlags, Binder, Object[], CultureInfo)

When implemented by a subclass, this method invokes the reflected method with the given parameters.

[Visual Basic]
Overloads MustOverride Public Function Invoke( _
   ByVal obj As Object, _
   ByVal invokeAttr As BindingFlags, _
   ByVal binder As Binder, _
   ByVal parameters() As Object, _
   ByVal culture As CultureInfo _
) As Object
[C#]
public abstract object Invoke(
   object obj,
   BindingFlags invokeAttr,
   Binder binder,
   object[] parameters,
   CultureInfo culture
);
[C++]
public: virtual Object* Invoke(
   Object* obj,
   BindingFlags invokeAttr,
   Binder* binder,
   Object* parameters[],
   CultureInfo* culture
) = 0;
[JScript]
public abstract function Invoke(
   obj : Object,
   invokeAttr : BindingFlags,
   binder : Binder,
   parameters : Object[],
   culture : CultureInfo
) : Object;

Parameters

obj
The object on which to invoke the method. If the method is static, this argument is ignored.
invokeAttr
[To be supplied.]
binder
[To be supplied.]
parameters
[To be supplied.]
culture
[To be supplied.]

Return Value

Returns a Variant containing the return value of the invoked method, or a null reference (in Visual Basic Nothing) if the method's return type is void. For example, if the method returns a Boolean, an instance of Variant with the appropriate Boolean value is returned. If the method has a void return type, a null reference (Nothing) is returned. Before calling the method, Invoke will check to see if the user has access permission and verify that the parameters are legal.

Exceptions

Exception Type Condition
TargetException obj is null.

The method is non-static if the method is not declared or inherited by the class of obj.

ArgumentException The number, type and order of parameters do not match the signature of the method reflected by this instance.
AccessException The caller does not have permission to invoke the method.

The method invoked is a class initializer.

TargetInvocationException The invoked method throws an exception. TargetInvocationException.GetException() returns the exception.

Remarks

Dynamically invokes the method reflected by this instance on obj, and passes along the specified parameters. If the method is static, the obj parameter is ignored. For non-static methods, obj should be an instance of a class that inherits or declares the method and must be the same type as this class. If the method has no parameters, the value of the parameters parameter should be null. Otherwise the number, type, and order of elements in the parameters array should be identical to the number, type, and order of parameters for the method reflected by this instance.

You may not omit optional parameters in calls to Invoke. To invoke a method omitting optional parameters, you should call Type.InvokeMember instead.

For pass-by-value primitive parameters, normal widening are preformed (Int16-> Int32 for example). For pass-by-value reference parameters, normal reference widening are allowed (Derived class to base class and base class to interface type). However for pass-by-reference primitive parameters the types must match exactly. For pass-by-reference reference parameters the normal widening still apply.

For example, if the method reflected by this instance is declared as public boolean Compare(String a, String b), then parameters should be an array of Variants with length 2 such that parameters[0] = new Variant("SomeString1") and parameters[1] = new Variant("SomeString2").

Note that reflection uses dynamic method lookup when invoking virtual methods. For example, suppose that class B inherits from class A and both implement a virtual method named M. Now suppose that you have a MethodInfo object that represents M on class A. If you use the Invoke method to invoke M on an object of type B, then reflection will use the implementation given by class B. Even if the object of type B is cast to A, the implementation given by class B is used (see code sample below).

On the other hand, if the method is non-virtual then reflection will use the implementation given by the type from which the MethodInfo was obtained, regardless of the type of the object passed as the target.

Access restrictions are ignored for fully trusted code. That is, private constructors, methods, fields, and properties can be accessed and invoked via reflection whenever the code is fully trusted.

If the invoked method throws an exception, TargetInvocationException.GetException() returns the exception. This implementation throws NotSupportedException.

Example [C#]

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.

[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

See Also

MethodInfo Class | MethodInfo Members | System.Reflection Namespace | MethodInfo.Invoke Overload List