C # Reflection

Source: Internet
Author: User

The knowledge reflected in C # is used very closely with the type class. To say the direction of reflection, in fact, MVC is the use of reflective knowledge. In addition, if you want to do plug-ins, the knowledge of reflection is also essential.

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 class:

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" (No parameters)   Perform the operation! ");         }        public  void dosomething ( string mask )         {             console.writeline ("Do.DoSomeThing" (with parameters)   Perform the operation! {0} ",  mask);         }    }}

about the console tester:

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");//load the assembly and parse the Matedata            type inside  _ty = _assembly. GetType ("testdll.do");             //------instantiation of the article             object _ob =  Activator.CreateInstance (_ty);//Instantiate an object  ,  equivalent of New        &nbsP;   object _oboverride = activator.createinstance (_ty,  "Ainy",  27);// Instantiate an overloaded constructor with parameters             ///////////////////////              //this _ob (Object) is actually do             Do _testDo = _ob as Do;             _testdo.dosomething ();             ///////////////////////////////////////////////////////////////////////////////             foreach (methodinfo _methoditem in _ty. GetMethods ())             {                 console.writeline ("Method  : {0}"  , _methoditem.name) ;            }             //------Method Article              methodinfo _hasparm = _ty. GetMethod ("DoSomething",  new type[] { typeof (String)  });//Get a method with a String parameter   : dosomething            //Execution Method  :  DoSomething (parametric)             _hasparm.invoke (_ob,  new object[] {  "Aonaufly"  });             methodinfo _noparm = _ty. GetMethod ("DoSomething", new type[]{});//Get the method without parameters              _noparm.inVoke (_ob,new object[]{});//execute parameterless method              //------Field Chapter            fieldinfo _name =  _ty. GetField ("Name");            _name. SetValue (_ob,   "Aonaufly1");             ------Attribute Chapter            propertyinfo _age =  _ty. GetProperty ("Age");            _age. SetValue (_ob, 18);                         console.readkey ();         }    }}

Issues to be noted:

1: The dll,pdb file of the class library is to be copied to the bin file of the assembly. The PDB file allows the program to enter debugging.

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=" Class library dll/pdb location "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=" Console dll/pdb storage Location "alt=" wkiom1f8lbgzqt1aaab0ymdswdi296.png-wh_50 "/>

2:assembly.load ("Testdll")

The actual is to load the assembly (and parse the Matedata data). The assembly is: 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=" can be the name of the assembly "alt=" Wkiol1f8lmgdq0g7aaad_udfkw0054.png-wh_50 "/>

3:_assembly. GetType ("Testdll.do")

Home in the Do class in order to invoke the


All right, look at the results of the execution:

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 "/>

This article is from the "Better_power_wisdom" blog, make sure to keep this source http://aonaufly.blog.51cto.com/3554853/1860664

C # Reflection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.