前言
獲取方法的相關信息的兩種形式
反射是一種允許用戶獲得類信息的C#功能,Type對象映射它代表的底層對象;
在.Net 中, 一旦獲得了Type對象,就可以使用GetMethods()方法獲取此類型支持的方法列表;該方法的兩種形式:
MethodInfo [] GetMethods()
MethodInfo [] GetMethods(BindingFlags bindingflas) :它的參數帶有一些限制BindingFlags 是一個枚舉
枚舉成員 [DeclaredOnly,Instance ,Public] 枚舉成員的功能使用 在編譯器中使用"."符號后自己認真觀察 【相信你很快能夠理解】
ParameterInfo[] GetParameters() 方法返回一個方法的參數列表
下面用到的類 MyClass ,為了方便閱讀,我把它折疊了!
1 class MyClass 2 { 3 int x; 4 int y; 5 public MyClass(int i, int j) 6 { 7 this.x = i; 8 this.y = j; 9 }10 public int Sum()11 {12 return x + y;13 }14 public bool IsBetween(int i)15 {16 if (x < i && i < y)17 {18 return true;19 }20 return false;21 }22 public void Set(int a, int b)23 {24 x = a;25 y = b;26 }27 public void Set(double a, double b)28 {29 x = (int)a;30 y = (int)b;31 }32 public void Show()33 {34 Console.WriteLine("x: " + x + " y: " + y);35 }36 }MyClass
Main:
1 Type t = typeof(MyClass);//獲得一個表示MyClass類的Type對象 2 Console.WriteLine("獲取當前成員的名稱" + t.Name); 3 Console.WriteLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); 4 Console.WriteLine("支持的方法"); 5 #region 第一種形式 6 //MethodInfo[] mi = t.GetMethods();//顯示Class類中被支持的方法 7 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 8 //方法GetMethods() 把 MyClass 的基類 object方法都顯示出來了 9 //下面我們說說 GetMethods() 的另外一種形式,有限制的顯示10 #endregion11 #region 第二種形式12 MethodInfo[] mi = t.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);13 #endregion14 foreach (MethodInfo m in mi)15 {16 //返回后打印出MyClass類中成員的類型(方法的返回值類型)極其方法名稱17 Console.Write(" " + m.ReturnType.Name + " " + m.Name + " (");//ReturnType獲取此方法的返回類型18 ParameterInfo[] pi = m.GetParameters();//獲得方法的參數19 for (int i = 0; i < pi.Length; i++)20 {21 Console.Write(pi[i].ParameterType.Name + " " + pi[i].Name);//ParameterType 獲取該參數的Type(類型)22 if (i+1<pi.Length)23 {24 Console.Write(", ");25 }26 27 }28 Console.WriteLine(")");29 Console.WriteLine();30 } 36 38 Console.ReadKey();
使用反射調用方法
上面 討論了怎么獲取一個類型所支持的方法,然而為我們獲取對方法的調用做了充分的準備!
MethodInfo類中的Invoke() 方法提供了該技能!
它的一種形式: object Invoke(object obj,object [] paramenters)
obj 是一個對象引用,將調用它所指向的對象上的方法,對于static方法,obj必須為null。
所有需要傳遞給方法的參數都必須在parameters數組中指定。如果方法不需要參數,則paramenters必須為null
基類MethodBase的Invoke()方法返回被調用方法的返回值
請看下面的事例:
MyClass類Set()方法有所改變:
1 public void Set(int a, int b) 2 { 3 Console.WriteLine("Set(int,int)"); 4 x = a; 5 y = b; 6 Show(); 7 } 8 public void Set(double a, double b) 9 {10 Console.WriteLine("Set(double,double)");11 x = (int)a;12 y = (int)b;13 Show();14 }
應用程序代碼!
1 Type t = typeof(MyClass); 2 MyClass reflectOb = new MyClass(10, 20); 3 int val; 4 Console.WriteLine("Invoke methods in " + t.Name);//調用MyClass類的方法 5 Console.WriteLine(); 6 MethodInfo[] mi = t.GetMethods(); 7 8 foreach (MethodInfo m in mi)//調用每個方法 9 {10 //獲得方法參數11 ParameterInfo[] pi = m.GetParameters();12 if (m.Name.Equals("Set",StringComparison.Ordinal)&&pi[0].ParameterType==typeof(int))13 {14 // 指定 System.String.Compare(System.String,System.String) 和 System.String.Equals(System.Object)15 // 方法的某些重載要使用的區域、大小寫和排序規則。16 //StringComparison.Ordinal 使用序號排序規則比較字符串 17 object[] obj = new object[2];18 obj[0] = 9;19 obj[1] = 18;20 m.Invoke(reflectOb, obj);21 }22 else if (m.Name.Equals("Set",StringComparison.Ordinal)&&pi[0].ParameterType==typeof(double))23 {24 object[] obj = new object[2];25 obj[0] = 1.12;26 obj[1] = 23.4;27 m.Invoke(reflectOb, obj);28 }29 else if (m.Name.Equals("Sum",StringComparison.Ordinal))30 {31 val = (int)m.Invoke(reflectOb, null);32 Console.WriteLine("Sum is : " + val);33 }34 else if (m.Name.Equals("IsBetween", StringComparison.Ordinal))35 {36 object[] obj = new object[1];37 obj[0] = 14;38 if ((bool)m.Invoke(reflectOb, obj))39 {40 Console.WriteLine("14 is between x and y"); 41 }42 }43 else if (m.Name.Equals("Show",StringComparison.Ordinal))44 {45 m.Invoke(reflectOb,null);46 }47 }Main
詳細地址 :http://www.mhPRofessional.com/product.php?cat=112&isbn=007174116X&cat=112
新聞熱點
疑難解答