Type.InvokeMember Method (String, BindingFlags, Binder, Object, Object[])

Invokes the specified member, using the specified binding constraints and matching the specified argument list.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public Object InvokeMember(
	string name,
	BindingFlags invokeAttr,
	Binder binder,
	Object target,
	Object[] args


Type: System.String
The String containing the name of the constructor, method, property, or field member to invoke.
An empty string ("") to invoke the default member.
Type: System.Reflection.BindingFlags
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.
Type: System.Reflection.Binder
A Binder 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.
A null reference (Nothing in Visual Basic), to use the DefaultBinder. Note that explicitly defining a Binder object may be requird for successfully invoking method overloads with variable arguments.
Type: System.Object
The Object on which to invoke the specified member.
Type: System.Object[]
An array containing the arguments to pass to the member to invoke.

Return Value

Type: System.Object
An Object representing the return value of the invoked member.


invokeAttr contains CreateInstance and typeName is null.


args is multidimensional.


invokeAttr is not a valid BindingFlags attribute.


invokeAttr does not contain one of the following binding flags: InvokeMethod, CreateInstance, GetField, SetField, GetProperty, or SetProperty.


invokeAttr contains CreateInstance combined with InvokeMethod, GetField, SetField, GetProperty, or SetProperty.


invokeAttr contains both GetField and SetField.


invokeAttr contains both GetProperty and SetProperty.


invokeAttr contains InvokeMethod combined with SetField or SetProperty.


invokeAttr contains SetField and args has more than one element.


This method is called on a COM object and one of the following binding flags was not passed in: BindingFlags.InvokeMethod, BindingFlags.GetProperty, BindingFlags.SetProperty, BindingFlags.PutDispProperty, or BindingFlags.PutRefDispProperty.


One of the named parameter arrays contains a string that is null.


The specified member is a class initializer.


The field or property cannot be found.


The method cannot be found.


The current Type object represents a type that contains open type parameters, that is, ContainsGenericParameters returns true.


The specified member cannot be invoked on target.


More than one method matches the binding criteria.


The .NET Compact Framework does not currently support this method.


The method represented by name has one or more unspecified generic type parameters. That is, the method's ContainsGenericParameters property returns true.

In Silverlight, only accessible members can be invoked through reflection.


You cannot use InvokeMember to invoke a generic method.

The following BindingFlags filter flags can be used to define which members to include in the search:

  • Specify BindingFlags.Public to include public members in the search.

  • Specify BindingFlags.NonPublic to include non-public members (that is, private and protected members) in the search.

  • Specify BindingFlags.FlattenHierarchy to include static members up the hierarchy.

The following BindingFlags modifier flags can be used to change how the search works:

  • BindingFlags.IgnoreCase to ignore the case of name.

  • BindingFlags.DeclaredOnly to search only the members declared on the Type, not members that were simply inherited.

The following BindingFlags invocation flags can be used to denote what action to take with the member:

  • CreateInstance to invoke a constructor. name is ignored. Not valid with other invocation flags.

  • InvokeMethod to invoke a method, but not a constructor or a type initializer. Not valid with SetField or SetProperty. If InvokeMethod is specified by itself, BindingFlags.Public, BindingFlags.Instance, and BindingFlags.Static are automatically included.

  • GetField to get the value of a field. Not valid with SetField.

  • SetField to set the value of a field. Not valid with GetField.

  • GetProperty to get a property. Not valid with SetProperty.

  • SetProperty to set a property. Not valid with GetProperty.

See System.Reflection.BindingFlags for more information.

A method will be invoked if the following conditions are true:

  • The number of parameters in the method declaration equals the number of arguments in the args array (unless default arguments are defined on the member), and

  • The type of each argument can be converted by the binder to the type of the parameter.

The binder will find all of the matching methods. These methods are found based upon the type of binding requested (BindingFlags values InvokeMethod, GetProperty, and so on). The set of methods is filtered by the name, number of arguments, and a set of search modifiers defined in the binder.

After the method is selected, it is invoked. Accessibility is checked at that point. The search may control which set of methods are searched based upon the accessibility attribute associated with the method. The Binder.BindToMethod method of the Binder class is responsible for selecting the method to be invoked. The default binder selects the most specific match.

Access restrictions are ignored for fully trusted code; that is, private constructors, methods, fields, and properties can be accessed and invoked through System.Reflection whenever the code is fully trusted.

You can use Type.InvokeMember to set a field to a particular value by specifying BindingFlags.SetField. For example, if you want to set a public instance field named F on class C, and F is a String, you can use code such as:

typeof(C).InvokeMember("F", BindingFlags.SetField, null, C, new Object{"strings new value"});

If F is a String[], you can use code such as:

typeof(C).InvokeMember("F", BindingFlags.SetField, null, C, new Object{new String[]{"a","z","c","d"});

which will initialize the field F to this new array. You can also use Type.InvokeMember to set a position in an array by supplying the index of the value and then the next value by using code such as the following:

typeof(C).InvokeMember("F", BindingFlags.SetField, null, C, new Object{1, "b"});

This will change string "z" in the array that F holds to string "b".

When you invoke an IDispatch member, you can specify the DispID instead of the member name, using the string format "[DispID=##]". For example, if the DispID of MyComMethod is 3, you can specify the string "[DispID=3]" instead of "MyComMethod". Invoking a member by DispID is faster than looking up the member by name. In complex aggregation scenarios, the DispID is sometimes the only way to invoke the desired member.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 If you attempt to set a non-integer value to an integer field of a base class through a derived class, InvokeMember throws MissingFieldException instead of ArgumentException.

If your application attempts to invoke a non-existing method of a type, or tries to invoke a method that has ambiguity, Silverlight for Windows Phone throws AmbiguousMatchException.

If name contains a wildcard character, InvokeMember throws a MissingFieldException.

When the current type object contains open type parameters, Type.InvokeMember(String, BindingFlags, Binder, Object, Object[]) throws NotSupportedException instead of InvalidOperationException.

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

using System;
using System.Reflection;

// This sample class has a field, constructor, method, and property.
class MyType
   public Int32 myField;
   public MyType(ref Int32 x) { x *= 5; }
   public override String ToString() { return myField.ToString(); }

   private Int32 myPropValue;
   public Int32 MyProp
      get { return myPropValue; }
         if (value < 1)
            throw new ArgumentOutOfRangeException("value", "value must be > 0");
         myPropValue = value;

class Example
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      Type t = typeof(MyType);

      // Arguments for the constructor. The parameter is ByRef, so the
      // constructor changes the value.
      Object[] args = new Object[] { 8 };
      outputBlock.Text += 
         String.Format("The value of x before the constructor is called is {0}.\n",

      // Invoke the constructor to create an instance of the type.
      Object obj = t.InvokeMember(null,
          BindingFlags.DeclaredOnly | BindingFlags.Public | 
             BindingFlags.Instance | BindingFlags.CreateInstance, 
          null, null, args);

      outputBlock.Text += "Type: " + obj.GetType().ToString() + "\n";
      outputBlock.Text += 
         String.Format("The value of x after the constructor returns is {0}.\n", 

      // Read and write to a field.
          BindingFlags.DeclaredOnly | BindingFlags.Public |
             BindingFlags.Instance | BindingFlags.SetField, 
          null, obj, new Object[] { 5 });
      Int32 v = (Int32)t.InvokeMember("myField",
          BindingFlags.DeclaredOnly | BindingFlags.Public |
             BindingFlags.Instance | BindingFlags.GetField, 
          null, obj, null);
      outputBlock.Text += "myField: " + v + "\n";

      // Call a method.
      String s = (String)t.InvokeMember("ToString",
          BindingFlags.DeclaredOnly | BindingFlags.Public |
             BindingFlags.Instance | BindingFlags.InvokeMethod, 
          null, obj, null);
      outputBlock.Text += "ToString: " + s + "\n";

      // Read and write a property. First, attempt to assign an
      // invalid value; then assign a valid value; finally, get
      // the value.
         // Assign the value zero to MyProp. The property setter 
         // throws an exception, because zero is an invalid value.
         // InvokeMember catches the exception, and throws 
         // TargetInvocationException. To discover the real cause
         // you must catch TargetInvocationException and examine
         // the inner exception. 
             BindingFlags.DeclaredOnly | BindingFlags.Public |
                BindingFlags.Instance | BindingFlags.SetProperty, 
             null, obj, new Object[] { 0 });
      catch (TargetInvocationException e)
         // If the property assignment failed for some unexpected
         // reason, rethrow the TargetInvocationException.
         if (e.InnerException.GetType() !=
         outputBlock.Text += "An invalid value was assigned to MyProp." + "\n";
          BindingFlags.DeclaredOnly | BindingFlags.Public |
             BindingFlags.Instance | BindingFlags.SetProperty, 
          null, obj, new Object[] { 2 });
      v = (Int32)t.InvokeMember("MyProp",
          BindingFlags.DeclaredOnly | BindingFlags.Public |
             BindingFlags.Instance | BindingFlags.GetProperty, 
          null, obj, null);
      outputBlock.Text += "MyProp: " + v + "\n";

/* This example produces output similar to the following:

The value of the argument before the constructor is called is 8.
Created instance of type: MyType
The value of the argument after the constructor is called is 40.
MyField: 5
ToString: 5
An invalid value was assigned to MyProp.
MyProp: 2


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions