獲取Type對象的構造函數:
前一篇因為篇幅問題因為篇幅太短被移除首頁,反射這一塊還有一篇“怎樣在程序集中使用反射”,
其他沒有什么可以寫的了,前兩篇主要是鋪墊,在實際應用中,主要是最后兩篇。其實寫完反射,
我也考慮重新把委托重新寫下,現在想想確實太過單一和簡單了。我會把實際開發中委托的例子多寫幾個
, 以便以理解。如果這幾篇C#的基礎對您有所幫助的話,請點擊推薦。謝謝您的支持……
好了,不廢話了,在前面例子中,由於MyClass類型的對象是顯示創建的,因此使
用反射技術來調用MyClass上的方法沒有任何優勢--以普通的方式調用對象上的方法會簡單的多。但是,
如果對象是在運行時動態創建的,反射的功能就顯示出來了。在這種情況下,需要首先獲取一個構造函
數列表,然后再調用列表中的某個構造函數,創建一個該類型的實例。通過這種機制,可以在運行時實例
化任意類型的對象而不必在聲明中指定。
為了獲得某個類型的構造函數,需要調用Type對象上的GetConstructors()。常用形式為:
ConstructorInfo[] GetConstructors()
該方法返回一個描述構造函數的ConstructorInfo對象數組。ConstructorInfo中常用的
是GetParamters()方法,該方法返回給定構造函數的參數列表。
一旦找到了合適的構造函數,就調用ConstructorInfo定義的Invoke()方法來創建對象:
object Invoke(object[] args)
需要傳遞給此方法的所有參數都在args中指定。如果不需要參數,args必須為null。另外,
args必須包含與參數個數相同的元素,並且實參的類型必須與形參的類型兼容。Invoke()方法返回
的是指向新構造對象的引用。
例子:
測試對象類
class MyClass { int x; int y; public MyClass(int i) { Console.WriteLine("一個參數的構造函數:"); x = y = i; } public MyClass(int i, int j) { Console.WriteLine("兩個參數構造函數:"); x = i; y = j; Show(); } public int Sum() { return x + y; } public bool IsBetween(int i) { if (x < i && i < y) return true; else return false; } public void Set(int a, int b) { Console.Write("函數:Set(int a, int b)"); x = a; y = b; Show(); } public void Set(double a, double b) { Console.Write("函數:Set(double a, double b)"); x = (int)a; y = (int)b; Show(); } public void Show() { Console.WriteLine("x:{0},y:{1}", x, y); } }
使用反射:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace Reflection { class Program { static void Main(string[] args) { InvokeConsDemo(); Console.ReadKey(); } static void InvokeConsDemo() { Type t = typeof(MyClass); int val; ConstructorInfo[] ci = t.GetConstructors(); Console.WriteLine("類構造函數如下:"); foreach (ConstructorInfo c in ci) { Console.Write("" + t.Name + "("); ParameterInfo[] pi = c.GetParameters(); for (int i = 0; i < pi.Length; i++) { Console.Write(pi[i].ParameterType.Name + " " + pi[i].Name); if (i + 1 < pi.Length) Console.Write(", "); } Console.WriteLine(") "); } Console.WriteLine(); int x; for (x = 0; x < ci.Length; x++) { ParameterInfo[] pi = ci[x].GetParameters(); if (pi.Length == 2) break; } if (x == ci.Length) { Console.WriteLine("沒有找到兩個參數的構造函數"); return; } else { object[] consargs = new object[2]; consargs[0] = 10; consargs[1] = 20; object reflectOb = ci[x].Invoke(consargs); Console.WriteLine("用reflectOb調用方法"); Console.WriteLine(); MethodInfo[] mi = t.GetMethods(); foreach (MethodInfo m in mi) { ParameterInfo[] pi = m.GetParameters(); if (m.Name.CompareTo("Set") == 0 && pi[0].ParameterType == typeof(int)) { object[] args = new object[2]; args[0] = 12; args[1] = 7; m.Invoke(reflectOb, args); } else if (m.Name.CompareTo("Set") == 0 && pi[0].ParameterType == typeof(double)) { object[] args = new object[2]; args[0] = 1.25; args[1] = 7.5; m.Invoke(reflectOb, args); } else if (m.Name.CompareTo("Sum") == 0) { val = (int)m.Invoke(reflectOb, null); Console.WriteLine("Sum is {0}",val); } else if (m.Name.CompareTo("IsBetween") == 0) { object[] args = new object[1]; args[0] = 13; if ((bool)m.Invoke(reflectOb, args)) { Console.WriteLine("13 is between x and y"); } } else if (m.Name.CompareTo("Show") == 0) { m.Invoke(reflectOb, null); } } } } } }
運行結果為:
本例中,找到了一個兩個參數的構造函數,那么使用下面的語句實例化了一個該類型的對象:
object reflectOb=ci[x].Invoke(consargs);
調用Invoke()方法后,reflectOb將引用一個MyClass類型的對象。此后,程序將執行
reflectOb上的方法。
注意:本例為了簡單起見,假設了一個使用兩個參數的構造函數,並且兩個參數都為int類型
。但在實際的應用程序中,必須檢驗每一個參數的類型。