標籤:c# 反射
在C#中反射的知識和Type類一起運用得很緊密。要說反射的運用方向,其實MVC就是利用了反射的知識。另外,如果你想做外掛程式,反射的知識也是必不可少的。
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/88/98/wKiom1f8knbQBDTmAAB6YZGRJh0904.png-wh_500x0-wm_3-wmp_4-s_2065415274.png" title="01.png" alt="wKiom1f8knbQBDTmAAB6YZGRJh0904.png-wh_50" />
Do類:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace TestDll{ public class Do { public string Name; public event Action CallBack; public Do() { } public Do(String name , Int32 age) { this.Name = name; this.Age = age; } public Int32 Age { get; set; } public void DoSomeThing() { Console.WriteLine("Do.DoSomeThing(沒有參數) 執行操作!"); } public void DoSomeThing( string mask ) { Console.WriteLine("Do.DoSomeThing(有參數) 執行操作!{0}", mask); } }}
關於控制台測試程式:
using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using System.Text;using System.Threading.Tasks;using TestDll;namespace TestReflect{ class Program { static void Main(string[] args) { Assembly _assembly = Assembly.Load("TestDll");//載入程式集,並解析裡面的matedata Type _ty = _assembly.GetType("TestDll.Do"); //------執行個體化篇 object _ob = Activator.CreateInstance(_ty);//執行個體化一個對象 , 相當於New object _obOverride = Activator.CreateInstance(_ty, "Ainy", 27);//執行個體化一個有參數的重載建構函式 /////////////////////////////////////////////////////////////////////////////// //這個_ob(Object)實際上就是Do Do _testDo = _ob as Do; _testDo.DoSomeThing(); /////////////////////////////////////////////////////////////////////////////// foreach(MethodInfo _methodItem in _ty.GetMethods()) { Console.WriteLine("方法 : {0}" , _methodItem.Name); } //------方法篇 MethodInfo _hasParm = _ty.GetMethod("DoSomeThing", new Type[] { typeof(String) });//得到一個有String參數的方法 :DoSomeThing //執行方法 : DoSomeThing(有參數的) _hasParm.Invoke(_ob, new object[] { "Aonaufly" }); MethodInfo _noParm = _ty.GetMethod("DoSomeThing",new Type[]{});//擷取不帶參數的方法 _noParm.Invoke(_ob,new Object[]{});//執行無參數的方法 //------欄位篇 FieldInfo _name = _ty.GetField("Name"); _name.SetValue(_ob, "Aonaufly1"); //------屬性篇 PropertyInfo _age = _ty.GetProperty("Age"); _age.SetValue(_ob, 18); Console.ReadKey(); } }}
需要指出的問題:
1:類庫的DLL,PDB檔案要拷貝到程式集的Bin檔案中。PDB檔案可讓程式進入調試。
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M01/88/98/wKiom1f8lBCTgCq_AAAz-5k35y8104.png-wh_500x0-wm_3-wmp_4-s_1771697639.png" style="float:none;" title="類庫DLL/PDB位置" alt="wKiom1f8lBCTgCq_AAAz-5k35y8104.png-wh_50" />
650) this.width=650;" src="http://s2.51cto.com/wyfs02/M01/88/98/wKiom1f8lBGzQT1aAAB0ymDswdI296.png-wh_500x0-wm_3-wmp_4-s_1382391649.png" style="float:none;" title="控制台DLL/PDB存放位置" alt="wKiom1f8lBGzQT1aAAB0ymDswdI296.png-wh_50" />
2:Assembly.Load("TestDll")
實際是載入程式集(並解析matedata資料)。程式集即為:650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/88/95/wKioL1f8lMGDQ0g7AAAd_Udfkw0054.png-wh_500x0-wm_3-wmp_4-s_3063851507.png" title="可作為程式集的名稱" alt="wKioL1f8lMGDQ0g7AAAd_Udfkw0054.png-wh_50" />
3:_assembly.GetType("TestDll.Do")
家在Do類,以便調用
好了,看看執行結果:
650) this.width=650;" src="http://s5.51cto.com/wyfs02/M00/88/98/wKiom1f8lWaiXDXqAAAr21xw6Ds849.png-wh_500x0-wm_3-wmp_4-s_2052906943.png" title="05.png" alt="wKiom1f8lWaiXDXqAAAr21xw6Ds849.png-wh_50" />
本文出自 “Better_Power_Wisdom” 部落格,請務必保留此出處http://aonaufly.blog.51cto.com/3554853/1860664
C#反射