Introduction to reflection in C #

Source: Internet
Author: User

Purpose of reflection:



(1) Use Assembly to define and load the Assembly, load the module in the assembly list, and locate the type in this Assembly and create instances of this type.


(2) Use the module to understand the Assembly containing the module and the classes in the module, and obtain all global methods or other specific non-Global methods defined on the module.


(3) Use constructorinfo to understand the name, parameters, access modifiers (such as pulic or private) and implementation details (such as abstract or virtual) of the constructorinfo.


(4) use methodinfo to understand the method name, return type, parameters, access modifiers (such as pulic or private), and implementation details (such as abstract or virtual.


(5) Use fiedinfo to learn the field name, access modifier (such as public or private), implementation details (such as static), and obtain or set the field value.


(6) Use eventinfo to learn about the event name, event handler data type, custom attributes, declaration type, and reflection type, and add or remove event handlers.




Call methods through reflection



Protected void page_load (Object sender, eventargs E)

{

System. reflection. Assembly ass = assembly. loadfrom (appdomain. currentdomain. basedirectory + "bin \\\ webapplication1.dll"); // load DLL

System. type T = ass. GetType ("webapplication1.mainpage"); // obtain the type

 

String name = typeof (mainpage). assemblyqualifiedname;

System. Type T1 = type. GetType (name );

System. Type T2 = typeof (mainpage );

 

Object o = system. activator. createinstance (t); // create an instance

System. reflection. methodinfo MI = T. getmethod ("runjs1"); // obtain the Method

Mi. Invoke (O, new object [] {This. Page, "alert ('test reflection method')"}); // CALL THE METHOD

 

System. reflection. methodinfo Mi1 = T. getmethod ("runjs ");

Mi1.invoke (T, new object [] {This. Page, "alert ('test reflection mechanism 1')"}); // CALL THE METHOD

}




References: Reflection http://www.studyofnet.com/news/520.html in C #


This article from the "study also leisure" blog, please be sure to keep this source http://studyofnet.blog.51cto.com/8142094/1529631

Related Article

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.