C#的反射機制調用方法

.NET的反射(Reflection)是很是完善和強大的,例若有名的.NET反編譯工具Red Gate's .NET Reflector就是使用了.NET自身的反射機制,這裏有一個比較簡單的實例(使用控制檯程序),看看.NET中如何使用反射。
 數組

  
  
  
  
  1. using System;  
  2. using System.Reflection;  
  3.  
  4. namespace Mengliao.CSharp.C13.S02  
  5. {  
  6.     class MyClass  
  7.     {  
  8.         private int count;  
  9.  
  10.         public MyClass(int value)  
  11.         {  
  12.             count = value;  
  13.         }  
  14.  
  15.         public void m1()  
  16.         {  
  17.             Console.WriteLine("Called method 1.");  
  18.         }  
  19.  
  20.         public static int m2(int x)  
  21.         {  
  22.             return x * x;  
  23.         }  
  24.  
  25.         public void m3(int x, double y)  
  26.         {  
  27.             Console.WriteLine("Called method 3, paramaters: x = {0}, y = {1:E}.", x, y);  
  28.         }  
  29.  
  30.         public void m4()  
  31.         {  
  32.             Console.WriteLine("Called method 4. Count = {0}", count);  
  33.         }  
  34.  
  35.         private static string m5(double x) //私有靜態方法,不能直接調用,但能夠綁定到委託  
  36.         {  
  37.             return Math.Sqrt(x).ToString();  
  38.         }  
  39.     }  
  40.  
  41.     class Program  
  42.     {  
  43.         public static void Main()  
  44.         {  
  45.             //取得MyClass的Type對象,下面的代碼使用Type的靜態方法需指明程序集,做用相同  
  46.             //Type t = Type.GetType("Mengliao.CSharp.C13.S02.MyClass, ConsoleApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");  
  47.             Type t = typeof(MyClass);  
  48.             //經過Activator實例化MyClass,供實例方法調用  
  49.             object obj = Activator.CreateInstance(t, new object[] { 88 });  
  50.  
  51.             MethodInfo[] methods = t.GetMethods(); //獲取MyClass的全部方法列表  
  52.  
  53.             foreach (MethodInfo nextMethod in methods) //枚舉全部方法  
  54.             {  
  55.                 Console.WriteLine(nextMethod.ToString()); //顯示方法信息  
  56.                 if (nextMethod.Name == "m1"//方法m1  
  57.                 {  
  58.                     nextMethod.Invoke(obj, null); //使用obj對象調用方法m1,無參數  
  59.                 }  
  60.                 if (nextMethod.Name == "m2"//方法m2  
  61.                 {  
  62.                     //靜態方法,使用null調用方法m2,創建參數數組,傳入10  
  63.                     Console.WriteLine("Called static method 2, return {0}", nextMethod.Invoke(nullnew object[] { 10 }));  
  64.                 }  
  65.             }  
  66.  
  67.             MethodInfo m3Info = t.GetMethod("m3"); //獲取方法m3  
  68.             m3Info.Invoke(obj, new object[] { 123, 0.456 }); //調用方法m3,傳入對應的2個參數  
  69.  
  70.             //獲取方法m4,使用obj對象調用方法,無參數  
  71.             t.InvokeMember("m4", BindingFlags.InvokeMethod, null, obj, null);  
  72.  
  73.             //創建泛型委託runMe,並綁定MyClass的靜態私有方法m5  
  74.             Delegate runMe = Delegate.CreateDelegate(typeof(Func<doublestring>), t, "m5");  
  75.             Console.WriteLine("Call delegate with m5: Sqrt(2) = {0}", ((Func<doublestring>)runMe)(2)); //調用該委託  
  76.               
  77.             Console.ReadLine();  
  78.         }  
  79.     }  

使用反射機制能夠調用各類方法,包括私有的、靜態的等等,程序中的註釋很是詳細,這裏就很少說了。ide

相關文章
相關標籤/搜索