- 举例:
- Type tDate = typeof(System.DateTime);
- Object result = tDate.InvokeMember("Now",
- BindingFlags.GetProperty, null, null, new Object[0]);
- Console.WriteLine(result.ToString());
-
- 例2:
- TestClass tc = new TestClass ();
- tc.GetType().InvokeMember ("AddUp", BindingFlags.Public |
- BindingFlags.Instance | BindingFlags.CreateInstance,
- null, tc, new object [] {});
- Type t = typeof (TestClass);
- object [] args = new object [] {100.09, 184.45};
- object result = t.InvokeMember ("ComputeSum", BindingFlags.InvokeMethod, null, null, args);
-
-
- t.InvokeMember ("SayHello", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, new object [] {});
-
- TestClass c = new TestClass ();
- c.GetType().InvokeMember ("AddUp", BindingFlags.InvokeMethod, null, c, new object [] {});
-
- c.GetType().InvokeMember ("AddUp", BindingFlags.Public |
- BindingFlags.Instance | BindingFlags.CreateInstance,
- null, c, new object [] {});
-
-
- result = t.InvokeMember ("Name", BindingFlags.GetField | BindingFlags.GetProperty, null, c, new object [] {});
- result = t.InvokeMember ("Value", BindingFlags.GetField | BindingFlags.GetProperty, null, c, new object [] {});
- result = t.InvokeMember ("Name", BindingFlags.GetField, null, c, new object [] {});
-
- t.InvokeMember ("Name", BindingFlags.SetField, null, c, new object [] {"NewName"});
-
- object[] argValues = new object [] {"Mouse", "Micky"};
- String [] argNames = new String [] {"lastName", "firstName"};
- t.InvokeMember ("PrintName", BindingFlags.InvokeMethod, null, null, argValues, null, null, argNames);
-
- TestClass obj = new TestClass();
- System.Reflection.MethodInfo methInfo =
- obj.GetType().GetMethod("PrintName");
- methInfo.Invoke(obj,BindingFlags.SuppressChangeType |
- BindingFlags.InvokeMethod, null,new object[]
- {"Brad","Smith"},null);
-
- methInfo = obj.GetType().GetMethod("PrintName");
- methInfo.Invoke(obj,BindingFlags.IgnoreCase |
- BindingFlags.InvokeMethod, null,new object[]
- {"brad","smith"},null);
-
- methInfo = obj.GetType().GetMethod("PrintName");
- methInfo.Invoke(obj,BindingFlags.IgnoreReturn |
- BindingFlags.InvokeMethod, null,new object[]
- {"Brad","Smith"},null);
-
- methInfo = obj.GetType().GetMethod("PrintName");
- methInfo.Invoke(obj,BindingFlags.OptionalParamBinding |
- BindingFlags.InvokeMethod, null,new object[]
- {"Brad","Smith"},null);
-
-
- methInfo = obj.GetType().GetMethod("PrintName");
- methInfo.Invoke(obj,BindingFlags.ExactBinding |
- BindingFlags.InvokeMethod, null,new object[]
- {"Brad","Smith"},null);
-
-
- methInfo = obj.GetType().GetMethod("PrintName");
- methInfo.Invoke(obj,BindingFlags.FlattenHierarchy |
- BindingFlags.InvokeMethod, null,new object[]
- {"Brad","Smith"},null);
-
- Type t3 = typeof (TestClass2);
- t3.InvokeMember ("", BindingFlags.InvokeMethod | BindingFlags.Default, null, new TestClass2(), new object [] {});
- MethodInfo m = t.GetMethod("Swap");
- args = new object[2];
- args[0] = 1;
- args[1] = 2;
- m.Invoke(new TestClass(),args);
-
posted on
2018-03-30 16:03
无爱无伤
阅读(
444)
评论()
编辑
收藏
举报