C# TypeInfo InvokeMember(String, BindingFlags, Binder, Object, Object[])

Description

TypeInfo InvokeMember(String, BindingFlags, Binder, Object, Object[]) Invokes the specified member, using the specified binding constraints and matching the specified argument list.

Syntax

TypeInfo.InvokeMember(String, BindingFlags, Binder, Object, Object[]) has the following syntax.


public Object InvokeMember(
  string name,//  w w w . j  a v a2s.  com
  BindingFlags invokeAttr,
  Binder binder,
  Object target,
  Object[] args
)

Parameters

TypeInfo.InvokeMember(String, BindingFlags, Binder, Object, Object[]) has the following parameters.

  • name - The string containing the name of the constructor, method, property, or field member to invoke.
  • name - -or-
  • name - An empty string ("") to invoke the default member.
  • name - -or-
  • name - For IDispatch members, a string representing the DispID, for example "[DispID=3]".
  • invokeAttr - A bitmask comprised of one or more BindingFlags that specify how the search is conducted. The access can be one of the BindingFlags such as Public, NonPublic, Private, InvokeMethod, GetField, and so on. The type of lookup need not be specified. If the type of lookup is omitted, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static are used.
  • binder - An object that defines a set of properties and enables binding, which can involve selection of an overloaded method, coercion of argument types, and invocation of a member through reflection.
  • binder - -or-
  • binder - A null reference (Nothing in Visual Basic), to use the DefaultBinder. Note that explicitly defining a Binder object may be required for successfully invoking method overloads with variable arguments.
  • target - The object on which to invoke the specified member.
  • args - An array containing the arguments to pass to the member to invoke.

Returns

TypeInfo.InvokeMember(String, BindingFlags, Binder, Object, Object[]) method returns An object representing the return value of the invoked member.

Example

The following example uses InvokeMember to access members of a type.


//ww w  .  ja va  2  s  .  c  o m
using System;
using System.Reflection;

class MyType 
{
    Int32 myField;
    public MyType(ref Int32 x) {x *= 5;}
    public override String ToString() {return myField.ToString();}
    public Int32 MyProp 
    {
        get {return myField;}
        set 
        { 
            if (value < 1) 
                throw new ArgumentOutOfRangeException("value", value, "value must be > 0");
            myField = value;
        }
    }
}

class MyApp 
{
    static void Main() 
    {
        Type t = typeof(MyType);
        Object[] args = new Object[] {8};
        Object obj = t.InvokeMember(null, 
            BindingFlags.DeclaredOnly | 
            BindingFlags.Public | BindingFlags.NonPublic | 
            BindingFlags.Instance | BindingFlags.CreateInstance, null, null, args);
        Console.WriteLine("Type: " + obj.GetType().ToString());
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Reflection »




EventInfo
FieldInfo
MemberInfo
MethodInfo
ParameterInfo
TypeInfo