C # Reflections

Source: Internet
Author: User

Conceptual reflection is a capability that allows you to view metadata types and other information. The most basic data unit after metadata compilation. Essentially, it is about a large table Chase. After the program is compiled, many tables can be formed, such as class definition tables, field definition tables, and method definition tables. Reflection technology is to use tools in the recyclsioin namespace to allow you to parse the Code represented by these metadata. a pe file is called a portable executable and is the full name of PortableExecute. Common EXE, DLL, OCX, SYS, and COM files are all PE files, PE files are program files on Microsoft Windows operating systems (may be indirectly executed, such as DLL ). in. the pe file in the. net program contains two parts: Metadata Information and msIL information (msil and the part that marks metadata information .) The namespace used by the System. refloctionSystem. the main function of Type reflection is to expand the system and dynamically call the Assembly: extended system: the so-called extended system is to first write the system, the system defines the interface, the developer later writes the interface code. At this time, the system will use reflection, and the system will call the Interface Using Reflection. When the interface is not written, the system will not make an error using reflection, which means this function is not implemented at this time, when the interface is written, the system automatically calls the interface to display the function on the system. Reflection implements the plug-and-play function. Dynamic calling of the assembly dynamic calling of the Assembly is to use reflection to call the compiled dll. Of course, the dll is not referenced in your project. When you compile your project, you will not compile the dll you call. When you run it to call the dll, you will call the dll to determine whether there is syntax or semantics, running error. In this way, reflection is flexible, that is, you do not need to call the dll from your project, or the dll can be changed at any time (of course, the interface still needs to be matched), and you do not need to change the project you have created. Sample Code [csharp] class Program {static void Main (string [] args) {Console. WriteLine ("test thread reflection technology... "); Assembly ass; Type type; object obj; // 1 Load Assembly ass = Assembly. loadFile (@ "D: \ MYProject \ Thread_sTU \ DLL_reflection \ bin \ Debug \ DLL_reflection.dll"); // 2. Obtain the assembly type information based on the assembly information. note that this method is used to obtain information about various types (classes, methods, attributes, and so on) defined in metadata. Type = ass. getType ("DLL_reflection.Class1"); // 3 obtain the method object MethodInfo method = type based on the type information. getMethod ("text"); // 4 parameter object of the processing method object. Object [] paramobj = new object [0]; // 5 create an assembly instance obj = ass. createInstance ("DLL_reflection.Class1"); // 6 call this function based on the method object, instance object, and method output parameter object. Method. Invoke (obj, paramobj); Console. WriteLine ("test thread reflection technology END"); Console. ReadLine ();}}}

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.