C#使用反射機(jī)制實(shí)現(xiàn)延遲綁定
反射允許我們?cè)诰幾g期或運(yùn)行時(shí)獲取程序集的元數(shù)據(jù),通過(guò)反射可以做到:
● 創(chuàng)建類(lèi)型的實(shí)例
● 觸發(fā)方法
● 獲取屬性、字段信息
● 延遲綁定
......
如果在編譯期使用反射,可通過(guò)如下2種方式獲取程序集Type類(lèi)型:
- 1、Type類(lèi)的靜態(tài)方法
Type type = Type.GetType("somenamespace.someclass");
- 2、通過(guò)typeof
Type type = typeof(someclass);
如果在運(yùn)行時(shí)使用反射,通過(guò)運(yùn)行時(shí)的Assembly實(shí)例方法獲取Type類(lèi)型:
Type type = asm.GetType("somenamespace.someclass");
獲取反射信息
有這樣的一個(gè)類(lèi):
public class Student { public int Id { get; set; } public string Name { get; set; } public float Score { get; set; } public Student() { this.Id = -1; this.Name = string.Empty; this.Score = 0; } public Student(int id, string name, float score) { this.Id = id; this.Name = name; this.Score = score; } public string DisplayName(string name) { return string.Format("學(xué)生姓名:{0}", name); } public void ShowScore() { Console.WriteLine("學(xué)生分?jǐn)?shù)是:" + this.Score); } }
通過(guò)如下獲取反射信息:
static void Main(string[] args) { Type type = Type.GetType("ConsoleApplication1.Student"); //Type type = typeof (Student); Console.WriteLine(type.FullName); Console.WriteLine(type.Namespace); Console.WriteLine(type.Name); //獲取屬性 PropertyInfo[] props = type.GetProperties(); foreach (PropertyInfo prop in props) { Console.WriteLine(prop.Name); } //獲取方法 MethodInfo[] methods = type.GetMethods(); foreach (MethodInfo method in methods) { Console.WriteLine(method.ReturnType.Name); Console.WriteLine(method.Name); } Console.ReadKey(); }
延遲綁定
在通常情況下,為對(duì)象實(shí)例賦值是發(fā)生在編譯期,如下:
Student stu = new Student(); stu.Name = "somename";
而"延遲綁定",為對(duì)象實(shí)例賦值或調(diào)用其方法是發(fā)生在運(yùn)行時(shí),需要獲取在運(yùn)行時(shí)的程序集、Type類(lèi)型、方法、屬性等。
//獲取運(yùn)行時(shí)的程序集 Assembly asm = Assembly.GetExecutingAssembly(); //獲取運(yùn)行時(shí)的Type類(lèi)型 Type type = asm.GetType("ConsoleApplication1.Student"); //獲取運(yùn)行時(shí)的對(duì)象實(shí)例 object stu = Activator.CreateInstance(type); //獲取運(yùn)行時(shí)指定方法 MethodInfo method = type.GetMethod("DisplayName"); object[] parameters = new object[1]; parameters[0] = "Darren"; //觸發(fā)運(yùn)行時(shí)的方法 string result = (string)method.Invoke(stu, parameters); Console.WriteLine(result); Console.ReadKey();
到此這篇關(guān)于C#使用反射機(jī)制實(shí)現(xiàn)延遲綁定的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)字符串首字母大寫(xiě)的方法示例
這篇文章主要給大家介紹了關(guān)于利用C#實(shí)現(xiàn)字符串首字母大寫(xiě)的相關(guān)資料,這是在最近工作中遇到的一個(gè)需求,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01C# ListView 點(diǎn)擊表頭對(duì)數(shù)據(jù)進(jìn)行排序功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了C# ListView 點(diǎn)擊表頭對(duì)數(shù)據(jù)進(jìn)行排序功能的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-04-04C# 定時(shí)器保活機(jī)制引起的內(nèi)存泄露問(wèn)題解決
這篇文章主要介紹了C# 定時(shí)器?;顧C(jī)制引起的內(nèi)存泄露問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02基于C#的音樂(lè)播放器主Form實(shí)現(xiàn)代碼
這篇文章主要介紹了基于C#的音樂(lè)播放器主Form實(shí)現(xiàn)代碼,很實(shí)用的功能,需要的朋友可以參考下2014-08-08C# 撒列實(shí)現(xiàn)關(guān)鍵字過(guò)濾的實(shí)例
C# 撒列實(shí)現(xiàn)關(guān)鍵字過(guò)濾的實(shí)例,需要的朋友可以參考一下2013-04-04C#控制臺(tái)程序?qū)崿F(xiàn)開(kāi)啟、關(guān)閉SQLServer服務(wù)的代碼分享
這篇文章主要介紹了C#控制臺(tái)程序?qū)崿F(xiàn)開(kāi)啟、關(guān)閉SQLServer服務(wù)的代碼分享,需要的朋友可以參考下2014-05-05