函数原型:
public object InvokeMember(string, BindingFlags, Binder, object, object[]);
string:你所要调用的函数名
BindingFlags:你所要调用的函数的属性,可以组合
Binder:高级内容,可以先不看
object:调用该成员函数的实例
object[]:参数,
下面是msdn例子:
//调用公有静态成员函数(不带参数)
Type t = typeof (TestClass);
t.InvokeMember ("SayHello", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static, null, null, new object [] {});
//调用实例的函数(不带参数),第三个参数为该实例
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 [] {});
//调用带参数的函数,
//方法是:将你的所有参数都放到一个object的数组里面
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);
//获得一个属性值
result = t.InvokeMember ("Name", BindingFlags.Public | BindingFlags.GetField, null, c, new object [] {});
Console.WriteLine ("Name == {0}", result);
//设定一个属性值
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);
//获得一个下标属性值([])
int index = 3;
result = t.InvokeMember ("Item", BindingFlags.Public |BindingFlags.GetProperty , null, c, new object [] {index});
Console.WriteLine ("Item[{0}] == {1}", index, result);
//设定一个下标属性值([])
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);
//获得一个属性或者是成员变量的值
//也就是,假设有一个类是这样的:
//class temp{
// public string name;
// public string Name{
// get{return name;}
// set {name=value}
// }
//}
//那么通过一下语句就可获得Name的值,
result = t.InvokeMember ("Name", BindingFlags.Public |BindingFlags.GetField | BindingFlags.GetProperty, null, c, new object [] {});
Console.WriteLine ("Name == {0}", result);
//通过一下,语句可以获得name的值
result = t.InvokeMember ("name", BindingFlags.Public |BindingFlags.GetField | BindingFlags.GetProperty, null, c, new object [] {});
Console.WriteLine ("Value == {0}", result);
//调用一个函数,使用参数名对应的参数
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);
//调用一个类型的默认函数,好像在C#里面没有默认成员函数
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);
转载于:https://www.cnblogs.com/tsql/p/8676682.html