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

Description

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

Syntax

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


public Object InvokeMember(
  string name,/*w w w .  j av a2 s . co  m*/
  BindingFlags invokeAttr,
  Binder binder,
  Object target,
  Object[] args
)

Returns

Type.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.


/*from   w  ww .ja  va2 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 
        { 
            myField = value+1;
        }
    }
}

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());
        Console.WriteLine("The value of x after the constructor returns is {0}.", args[0]);

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version