本文发表在 rolia.net 枫下论坛Type.InvokeMember Method [C#]
See Also
Type Class | Type Members | System Namespace
Language
C#
C++
JScript
Visual Basic
Show All
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.
[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.
[Visual Basic] Overloads MustOverride Public 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#, JScript] Here are syntax examples using InvokeMember to invoke, get, and set various members.
[Visual Basic, C#, JScript] 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.IO
Imports System.Reflection
Public Class Sample
Public Sub Method()
'Call a static method
Dim t As Type = GetType(TestClass)
t.InvokeMember("SayHello", BindingFlags.Public Or _
BindingFlags.InvokeMethod Or BindingFlags.Static, Nothing, Nothing, _
New Object() {})
'Call an instance method
Dim c As New TestClass()
c.GetType().InvokeMember("AddUp", BindingFlags.Public Or _
BindingFlags.InvokeMethod, Nothing, c, New Object() {})
c.GetType().InvokeMember("AddUp", BindingFlags.Public Or _
BindingFlags.InvokeMethod, Nothing, c, New Object() {})
'Call a method with arguments
Dim args() As Object = {100.09, 184.45}
Dim result As Object
result = t.InvokeMember("ComputeSum", BindingFlags.Public Or _
BindingFlags.InvokeMethod Or BindingFlags.Static, Nothing, _
Nothing, args)
Console.WriteLine("{0} + {1} = {2}", args(0), args(1), result)
'Get a field value
result = t.InvokeMember("Name", BindingFlags.Public Or _
BindingFlags.GetField, Nothing, c, New Object() {})
Console.WriteLine("Name == {0}", result)
'Set a field
t.InvokeMember("Name", BindingFlags.Public Or _
BindingFlags.SetField, Nothing, c, New Object() {"NewName"})
result = t.InvokeMember("Name", BindingFlags.Public Or _
BindingFlags.GetField, Nothing, c, New Object() {})
Console.WriteLine("Name == {0}", result)
'Get an indexed property value
Dim index As Integer = 3
result = t.InvokeMember("Item", BindingFlags.Public Or _
BindingFlags.GetProperty, Nothing, c, New Object() {index})
Console.WriteLine("Item[{0}] == {1}", index, result)
'Set an indexed property value
index = 3
t.InvokeMember("Item", BindingFlags.Public Or _
BindingFlags.SetProperty, Nothing, c, New Object() {index, "NewValue"})
result = t.InvokeMember("Item", BindingFlags.Public Or _
BindingFlags.GetProperty, Nothing, c, New Object() {index})
Console.WriteLine("Item[{0}] == {1}", index, result)
'Get a field or property
result = t.InvokeMember("Name", BindingFlags.Public Or _
BindingFlags.GetField Or BindingFlags.GetProperty, Nothing, c, _
New Object() {})
Console.WriteLine("Name == {0}", result)
result = t.InvokeMember("Value", BindingFlags.Public Or _
BindingFlags.GetField Or BindingFlags.GetProperty, Nothing, c, _
New Object() {})
Console.WriteLine("Value == {0}", result)
'Call a method using named arguments
Dim argValues() As Object = {"Mouse", "Micky"}
Dim argNames() As String = {"lastName", "firstName"}
t.InvokeMember("PrintName", BindingFlags.Public Or _
BindingFlags.InvokeMethod, Nothing, Nothing, argValues, Nothing, _
Nothing, argNames)
'Call the default member of a type
Dim t3 As Type = GetType(TestClass2)
t3.InvokeMember("", BindingFlags.Public Or BindingFlags.InvokeMethod, _
Nothing, New TestClass2(), New Object() {})
'Invoking a ByRef member
Dim m As MethodInfo = t.GetMethod("Swap")
args = New Object(2) {}
args(0) = 1
args(1) = 2
m.Invoke(New TestClass(), args)
Console.WriteLine("{0}, {1}", args(0), args(1))
Console.WriteLine(Microsoft.VisualBasic.ControlChars.CrLf & _
"Press Return to exit.")
Console.Read()
End Sub
End Class
' Class added so sample will compile
Public Class TestClass
End Class
' Class added so sample will compile
Public Class TestClass2
End Class
[C#]
using System;
using System.IO;
using System.Reflection;
public class Sample
{
public void Method()
{
//Call a static method
Type t = typeof (TestClass);
t.InvokeMember ("SayHello", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static, null, null, new object [] {});
//Call an instance method
TestClass c = new TestClass ();
c.GetType().InvokeMember ("AddUp", BindingFlags.Public | BindingFlags.InvokeMethod, null, c, new object [] {});
c.GetType().InvokeMember ("AddUp", BindingFlags.Public | BindingFlags.InvokeMethod, null, c, new object [] {});
//Call a method with arguments
object [] args = new object [] {100.09, 184.45};
object result;
result = t.InvokeMember ("ComputeSum", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static, null, null, args);
Console.WriteLine ("{0} + {1} = {2}", args[0], args[1], result);
//Get a field value
result = t.InvokeMember ("Name", BindingFlags.Public | BindingFlags.GetField, null, c, new object [] {});
Console.WriteLine ("Name == {0}", result);
//Set a field
t.InvokeMember ("Name", BindingFlags.Public |BindingFlags.SetField, null, c, new object [] {"NewName"});
result = t.InvokeMember ("Name", BindingFlags.Public |BindingFlags.GetField, null, c, new object [] {});
Console.WriteLine ("Name == {0}", result);
//Get an indexed property value
int index = 3;
result = t.InvokeMember ("Item", BindingFlags.Public |BindingFlags.GetProperty , null, c, new object [] {index});
Console.WriteLine ("Item[{0}] == {1}", index, result);
//Set an indexed property value
index = 3;
t.InvokeMember ("Item", BindingFlags.Public |BindingFlags.SetProperty, null, c, new object [] {index, "NewValue"});
result = t.InvokeMember ("Item", BindingFlags.Public |BindingFlags.GetProperty , null, c, new object [] {index});
Console.WriteLine ("Item[{0}] == {1}", index, result);
//Get a field or property
result = t.InvokeMember ("Name", BindingFlags.Public |BindingFlags.GetField | BindingFlags.GetProperty, null, c, new object [] {});
Console.WriteLine ("Name == {0}", result);
result = t.InvokeMember ("Value", BindingFlags.Public |BindingFlags.GetField | BindingFlags.GetProperty, null, c, new object [] {});
Console.WriteLine ("Value == {0}", result);
//Call a method using named arguments
object[] argValues = new object [] {"Mouse", "Micky"};
String [] argNames = new String [] {"lastName", "firstName"};
t.InvokeMember ("PrintName", BindingFlags.Public |BindingFlags.InvokeMethod, null, null, argValues, null, null, argNames);
//Call the default member of a type
Type t3 = typeof (TestClass2);
t3.InvokeMember ("", BindingFlags.Public |BindingFlags.InvokeMethod, null, new TestClass2(), new object [] {});
//Invoking a ByRef member
MethodInfo m = t.GetMethod("Swap");
args = new object[2];
args[0] = 1;
args[1] = 2;
m.Invoke(new TestClass(),args);
Console.WriteLine ("{0}, {1}", args[0], args[1]);
Console.WriteLine ("\r\nPress Return to exit.");
Console.Read();
}
}
// Class added so sample will compile
public class TestClass {}
// Class added so sample will compile
public class TestClass2 {}更多精彩文章及讨论,请光临枫下论坛 rolia.net