首页 > 其他 > 详细

Type InvokeMember()用法简介

时间:2014-03-08 07:27:40      阅读:526      评论:0      收藏:0      [点我收藏+]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
举例:
  Type tDate = typeof(System.DateTime);
   Object result = tDate.InvokeMember("Now",
           BindingFlags.GetProperty, null, null, new Object[0]);
   Console.WriteLine(result.ToString());
 
  例2:  /*注意:下面备注的方法都是其他类的方法,比如:TestClass类方法*/
  TestClass tc = new TestClass (); //AddUp为tc的方法
  tc.GetType().InvokeMember ("AddUp", BindingFlags.Public |
               BindingFlags.Instance | BindingFlags.CreateInstance,
               null, tc, new object [] {});
   /*
       public void AddUp ()
       {
           methodCalled++;
           Console.WriteLine ("AddUp Called {0} times", methodCalled);
       }
   */
   //----------------------------下面传参数 执行ComputeSum方法 带有两个参数
   Type t = typeof (TestClass);
  object [] args = new object [] {100.09, 184.45};
  object result = t.InvokeMember ("ComputeSum", BindingFlags.InvokeMethod, null, null, args);
   /*
       public static double ComputeSum (double d1, double d2)
       {
           return d1 + d2;
       }
   */
  //-----------SayHello为静态方法调用
   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 [] {});
   /*
       public String Name;
       public Object Value
       {
           get
           {
               return "the value";
           }
       }
   */
   result = t.InvokeMember ("Name", BindingFlags.GetField, null, c, new object [] {});
  //---------设置字段
   t.InvokeMember ("Name", BindingFlags.SetField, null, c, new object [] {"NewName"}); //NewName设置的属性值
  //---------调用类方法 带参数
   object[] argValues = new object [] {"Mouse", "Micky"};
   String [] argNames = new String [] {"lastName", "firstName"};
   t.InvokeMember ("PrintName", BindingFlags.InvokeMethod, null, null, argValues, null, null, argNames);
   /*
       public static void PrintName (String firstName, String lastName)
       {
           Console.WriteLine ("{0},{1}", lastName,firstName);
       }
   */
   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 |  // 在 COM interop 中用于指定可以忽略成员的返回值
               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);
 
           // BindingFlags.ExactBinding
           methInfo = obj.GetType().GetMethod("PrintName");
           methInfo.Invoke(obj,BindingFlags.ExactBinding |
               BindingFlags.InvokeMethod, null,new object[]
               {"Brad","Smith"},null);
 
           // BindingFlags.FlattenHierarchy
           methInfo = obj.GetType().GetMethod("PrintName");
           methInfo.Invoke(obj,BindingFlags.FlattenHierarchy |
               BindingFlags.InvokeMethod, null,new object[]
               {"Brad","Smith"},null);
  //----------调用一个默认的方法
   Type t3 = typeof (TestClass2);
   /*
   [DefaultMemberAttribute ("PrintTime")]
   public class TestClass2
   {
       public void PrintTime ()
       {
           Console.WriteLine (DateTime.Now);
       }
   }
   */
   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);
    /*
       public void Swap(ref int a, ref int b)  交换 a b
       {
           int x = a;
           a = b;
           b = x;
       }
    */
   

  

Type InvokeMember()用法简介,布布扣,bubuko.com

Type InvokeMember()用法简介

原文:http://www.cnblogs.com/91loveme/p/3586380.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!