C#反射

來源:互聯網
上載者:User

標籤: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#反射

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.