Export (0) Print
Expand All
4 out of 5 rated this helpful - Rate this topic

Type.InvokeMember Method

Invokes a specific member of the current Type.

Overload List

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function InvokeMember(String, BindingFlags, Binder, Object, Object()) As Object
[C#] public object InvokeMember(string, BindingFlags, Binder, object, object[]);
[C++] public: Object* InvokeMember(String*, BindingFlags, Binder*, Object*, Object[]);
[JScript] public function InvokeMember(String, BindingFlags, Binder, Object, Object[]) : Object;

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

[Visual Basic] Overloads Public Function InvokeMember(String, BindingFlags, Binder, Object, Object(), CultureInfo) As Object
[C#] public object InvokeMember(string, BindingFlags, Binder, object, object[], CultureInfo);
[C++] public: Object* InvokeMember(String*, BindingFlags, Binder*, Object*, Object[], CultureInfo*);
[JScript] public function InvokeMember(String, BindingFlags, Binder, Object, Object[], CultureInfo) : Object;

When overridden in a derived class, invokes the specified member, using the specified binding constraints and matching the specified argument list, modifiers and culture.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public MustOverride Function InvokeMember(String, BindingFlags, Binder, Object, Object(), ParameterModifier(), CultureInfo, String()) As Object Implements IReflect.InvokeMember
[C#] public abstract object InvokeMember(string, BindingFlags, Binder, object, object[], ParameterModifier[], CultureInfo, string[]);
[C++] public: virtual Object* InvokeMember(String*, BindingFlags, Binder*, Object*, Object[], ParameterModifier[], CultureInfo*, String*[]) = 0;
[JScript] public abstract function InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]) : Object;

Example

[Visual Basic, C#, C++] The following example uses InvokeMember to access members of a type.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of InvokeMember. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Reflection

' This sample class has a field, constructor, method, and property.
Class MyType
    Private myField As Int32

    Public Sub New(ByRef x As Int32)
        x *= 5
    End Sub 'New

    Public Overrides Function ToString() As [String]
        Return myField.ToString()
    End Function 'ToString

    Public Property MyProp() As Int32
        Get
            Return myField
        End Get
        Set(ByVal Value As Int32)
            If Value < 1 Then
                Throw New ArgumentOutOfRangeException("value", Value, "value must be > 0")
            End If
            myField = Value
        End Set
    End Property
End Class 'MyType

Class MyApp

    Shared Sub Main()
        Dim t As Type = GetType(MyType)
        ' Create an instance of a type.
        Dim args() As [Object] = {8}
        Console.WriteLine("The value of x before the constructor is called is {0}.", args(0))
        Dim obj As [Object] = t.InvokeMember(Nothing, BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.CreateInstance, Nothing, Nothing, args)
        Console.WriteLine("Type: {0}", obj.GetType().ToString())
        Console.WriteLine("The value of x after the constructor returns is {0}.", args(0))

        ' Read and write to a field.
        t.InvokeMember("myField", BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.SetField, Nothing, obj, New [Object]() {5})
        Dim v As Int32 = CType(t.InvokeMember("myField", BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.GetField, Nothing, obj, Nothing), Int32)
        Console.WriteLine("myField: {0}", v)

        ' Call a method.
        Dim s As [String] = CType(t.InvokeMember("ToString", BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.InvokeMethod, Nothing, obj, Nothing), [String])
        Console.WriteLine("ToString: {0}", s)

        ' Read and write a property. First, attempt to assign an
        ' invalid value; then assign a valid value; finally, get
        ' the value.
        Try
            ' Assign the value zero to MyProp. The Property Set 
            ' 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. 
            t.InvokeMember("MyProp", BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.SetProperty, Nothing, obj, New [Object]() {0})
        Catch e As TargetInvocationException
            ' If the property assignment failed for some unexpected
            ' reason, rethrow the TargetInvocationException.
            If Not e.InnerException.GetType() Is GetType(ArgumentOutOfRangeException) Then
                Throw
            End If
            Console.WriteLine("An invalid value was assigned to MyProp.")
        End Try
        t.InvokeMember("MyProp", BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.SetProperty, Nothing, obj, New [Object]() {2})
        v = CType(t.InvokeMember("MyProp", BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.GetProperty, Nothing, obj, Nothing), Int32)
        Console.WriteLine("MyProp: {0}", v)
    End Sub 'Main
End Class 'MyApp

[C#] 
using System;
using System.Reflection;

// This sample class has a field, constructor, method, and property.
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);
        // Create an instance of a type.
        Object[] args = new Object[] {8};
        Console.WriteLine("The value of x before the constructor is called is {0}.", args[0]);
        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]);

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

        // Call a method.
        String s = (String) t.InvokeMember("ToString", 
            BindingFlags.DeclaredOnly | 
            BindingFlags.Public | BindingFlags.NonPublic | 
            BindingFlags.Instance | BindingFlags.InvokeMethod, null, obj, null);
        Console.WriteLine("ToString: " + s);

        // Read and write a property. First, attempt to assign an
        // invalid value; then assign a valid value; finally, get
        // the value.
        try 
        {
            // Assign the value zero to MyProp. The Property Set 
            // 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. 
            t.InvokeMember("MyProp", 
                BindingFlags.DeclaredOnly | 
                BindingFlags.Public | BindingFlags.NonPublic | 
                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() != 
                typeof(ArgumentOutOfRangeException)) 
                throw;
            Console.WriteLine("An invalid value was assigned to MyProp.");
        }
        t.InvokeMember("MyProp", 
            BindingFlags.DeclaredOnly | 
            BindingFlags.Public | BindingFlags.NonPublic | 
            BindingFlags.Instance | BindingFlags.SetProperty, null, obj, new Object[] {2});
        v = (Int32) t.InvokeMember("MyProp", 
            BindingFlags.DeclaredOnly | 
            BindingFlags.Public | BindingFlags.NonPublic | 
            BindingFlags.Instance | BindingFlags.GetProperty, null, obj, null);
        Console.WriteLine("MyProp: " + v);
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;

// This sample class has a field, constructor, method, and property.
__gc class MyType 
{
   Int32 myField;
public:
   MyType(Int32 __gc * x)
   {
      *x *= 5;
   }
   String* ToString()
   {
      return myField.ToString();
   }
   __property Int32 get_MyProp()
   {
         return myField;
   }
   __property void set_MyProp( Int32 value)
   { 
      if (value < 1) 
         throw new ArgumentOutOfRangeException(S"value", __box(value), S"value must be > 0");
      myField = value;
   }
};

int main() 
{
   Type* t = __typeof(MyType);
   // Create an instance of a type.
   Object* args[] = {__box(8)};
   Console::WriteLine(S"The value of x before the constructor is called is {0}.", args[0]);
   Object* obj = t->InvokeMember(0, 
      static_cast<BindingFlags>(BindingFlags::DeclaredOnly | BindingFlags::Public | BindingFlags::NonPublic | 
      BindingFlags::Instance | BindingFlags::CreateInstance), 0, 0, args);
   Console::WriteLine(S"Type: {0}", obj->GetType());
   Console::WriteLine(S"The value of x after the constructor returns is {0}.", args[0]);

   // Read and write to a field.
   Object* obj5[] = {__box(5)};
   t->InvokeMember(S"myField", 
      static_cast<BindingFlags>(BindingFlags::DeclaredOnly | 
      BindingFlags::Public | BindingFlags::NonPublic | 
      BindingFlags::Instance | BindingFlags::SetField), 0, obj, obj5);
   Int32 v = *__try_cast<Int32 __gc *>( t->InvokeMember(S"myField", 
      static_cast<BindingFlags>(BindingFlags::DeclaredOnly | 
      BindingFlags::Public | BindingFlags::NonPublic | 
      BindingFlags::Instance | BindingFlags::GetField), 0, obj, 0) );
   Console::WriteLine(S"myField: {0}", __box(v));

   // Call a method.
   String* s = __try_cast<String*>( t->InvokeMember(S"ToString", 
      static_cast<BindingFlags>(BindingFlags::DeclaredOnly | 
      BindingFlags::Public | BindingFlags::NonPublic | 
      BindingFlags::Instance | BindingFlags::InvokeMethod), 0, obj, 0) );
   Console::WriteLine(S"ToString: {0}", s);

   // Read and write a property. First, attempt to assign an
   // invalid value; then assign a valid value; finally, get
   // the value.
   try {
      // Assign the value zero to MyProp. The Property Set 
      // 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. 
      Object* obj0[] = {__box(0)};
      t->InvokeMember(S"MyProp", 
         static_cast<BindingFlags>(BindingFlags::DeclaredOnly | 
         BindingFlags::Public | BindingFlags::NonPublic | 
         BindingFlags::Instance | BindingFlags::SetProperty), 0, obj, obj0);
   } catch (TargetInvocationException* e) {
      // If the property assignment failed for some unexpected
      // reason, rethrow the TargetInvocationException.
      if (e->InnerException->GetType() != 
         __typeof(ArgumentOutOfRangeException)) 
         throw;
      Console::WriteLine(S"An invalid value was assigned to MyProp.");
   }
   Object* obj2[] = {__box(2)};
   t->InvokeMember(S"MyProp", 
      static_cast<BindingFlags>(BindingFlags::DeclaredOnly | 
      BindingFlags::Public | BindingFlags::NonPublic | 
      BindingFlags::Instance | BindingFlags::SetProperty), 0, obj, obj2);
   v = *__try_cast<Int32 __gc *>( t->InvokeMember(S"MyProp", 
      static_cast<BindingFlags>(BindingFlags::DeclaredOnly | 
      BindingFlags::Public | BindingFlags::NonPublic | 
      BindingFlags::Instance | BindingFlags::GetProperty), 0, obj, 0) );
   Console::WriteLine(S"MyProp: {0}", __box(v));
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Type Class | Type Members | System Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.