次の方法で共有


Type.InvokeMember メソッド

現在の Type の特定のメンバを呼び出します。

オーバーロードの一覧

指定したバインディング制約を使用し、指定した引数リストと照合して、指定したメンバを呼び出します。

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

指定したバインディング制約を使用し、指定したメンバのうち、指定した引数リストおよびカルチャと一致するメンバを呼び出します。

[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;

派生クラスによってオーバーライドされた場合、指定したバインディング制約を使用し、指定したメンバのうち、指定した引数リスト、修飾子、およびカルチャと一致するメンバを呼び出します。

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

使用例

[Visual Basic, C#, C++] InvokeMember を使用して型のメンバにアクセスする例を次に示します。

[Visual Basic, C#, C++] メモ   ここでは、InvokeMember のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

Type クラス | Type メンバ | System 名前空間