IReflect.InvokeMember Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Invokes a specified member.

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

Syntax

'Declaration
Function InvokeMember ( _
    name As String, _
    invokeAttr As BindingFlags, _
    binder As Binder, _
    target As Object, _
    args As Object(), _
    modifiers As ParameterModifier(), _
    culture As CultureInfo, _
    namedParameters As String() _
) As Object
Object InvokeMember(
    string name,
    BindingFlags invokeAttr,
    Binder binder,
    Object target,
    Object[] args,
    ParameterModifier[] modifiers,
    CultureInfo culture,
    string[] namedParameters
)

Parameters

  • invokeAttr
    Type: System.Reflection.BindingFlags
    One of the BindingFlags invocation attributes. The invokeAttr parameter may be a constructor, method, property, or field. A suitable invocation attribute must be specified. Invoke the default member of a class by passing the empty string ("") as the name of the member.
  • target
    Type: System.Object
    The object on which to invoke the specified member. This parameter is ignored for static members.
  • args
    Type: array<System.Object[]
    An array of objects that contains the number, order, and type of the parameters of the member to be invoked. This is an empty array if there are no parameters.
  • modifiers
    Type: array<System.Reflection.ParameterModifier[]
    An array of ParameterModifier objects. This array has the same length as the args parameter, representing the invoked member's argument attributes in the metadata. A parameter can have the following attributes: pdIn, pdOut, pdRetval, pdOptional, and pdHasDefault. These represent [In], [Out], [retval], [optional], and a default parameter, respectively. These attributes are used by various interoperability services.
  • culture
    Type: System.Globalization.CultureInfo
    An instance of CultureInfo used to govern the coercion of types. For example, culture converts a String that represents 1000 to a Double value, since 1000 is represented differently by different cultures. If this parameter is nulla null reference (Nothing in Visual Basic), the CultureInfo for the current thread is used.
  • namedParameters
    Type: array<System.String[]
    A String array of parameters.

Return Value

Type: System.Object
The specified member.

Exceptions

Exception Condition
ArgumentException

invokeAttr is BindingFlags.CreateInstance and another bit flag is also set.

ArgumentException

invokeAttr is not BindingFlags.CreateInstance and name is nulla null reference (Nothing in Visual Basic).

ArgumentException

invokeAttr is not an invocation attribute from BindingFlags.

ArgumentException

invokeAttr specifies both get and set for a property or field.

ArgumentException

invokeAttr specifies both a field set and an Invoke method. args are provided for a field get.

ArgumentException

More than one argument is specified for a field set.

MissingFieldException

The field or property cannot be found.

MissingMethodException

The method cannot be found.

MethodAccessException

The member that is being invoked is not accessible to the caller.

-or-

The member that is being invoked, or the type that contains that member, has the SecurityCriticalAttribute attribute.

Remarks

The method that is to be invoked must be accessible and provide the most specific match with the specified argument list, under the constraints of the specified binder and invocation attributes.

A method is invoked if the number of parameters in the method declaration equals the number of arguments in the specified argument list, and the type of each argument can be converted by the binder to the type of the parameter.

NoteNote:

The array of parameter modifiers passed to the InvokeMember method must contain a single parameter modifier. Only the first parameter modifier is considered when determining which argument needs to be passed by reference when exposed to COM.

The binder finds all matching methods, in accordance with the type of binding requested (BindingFlags.InvokeMethod, GetProperties, 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, and accessibility is checked at that point. The search may control which set of methods is searched based upon the accessibility attribute associated with the method. BindToMethod selects 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 reflection whenever the code is fully trusted.

Examples

The following example obtains the value of the Now property.

Imports System.Reflection

Public Class Example
   Public Overloads Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim tDate As Type = GetType(System.DateTime)
      Dim result As [Object] = tDate.InvokeMember("Now", _
          BindingFlags.GetProperty, Nothing, Nothing, New [Object](-1) {})
      outputBlock.Text &= result.ToString() & vbCrLf
   End Sub 'Main
End Class 'MainClass
using System;
using System.Reflection;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Type tDate = typeof(System.DateTime);
      Object result = tDate.InvokeMember("Now",
          BindingFlags.GetProperty, null, null, new Object[0]);
      outputBlock.Text += result.ToString() + "\n";
   }
}

Version Information

Silverlight

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

Platforms

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