Reflection? Is it physical light reflection ?? NO, NO, NO !!! This "reflection" has nothing to do with physics !!
So what is reflection? In my personal understanding, the function of dynamically adding a program (adding a dll file) in the program does not need to be added to the source code to "Upgrade" the function of the program ".
Here are some official examples.
If we have all played games, the game manufacturer will upgrade the game over a period of time, such as music and tracks. The client of QQ speed has been downloaded to us.
It is impossible for us to download the client again every time the game is upgraded. What should we do if this is the case?
When the game is upgraded, did you notice that there are several more. dll files in the folder where the game is installed?
If you haven't noticed it before, you can check it now. That's the legendary "reflection "!!
Dizzy !! Just a few more files. Can there be so much music and so many complicated tracks in QQ speed?
That's right. Let me talk about the reflection principle below. My personal understanding is superficial:
When we write the main program, for example, the functions in the main program of QQ speed are not completely written, but there is something similar to the external interface.
The purpose is to add more functions for the main program in the future.
The main program consists of the following steps:
1. Search all. dll files in the dll Directory when loading the form.
2. Obtain the class with the plug-in function in the. dll file.
3. Implement function extension in the program by calling the class methods in the plug-in
4. the developer of the main program must make a convention: all developers of plug-ins must name the method executed by the plug-in as a fixed name.
5. the developer of the main program, no matter how many classes are defined by the developer plug-in, and how many methods can be used to call only the specified method in the main program. This is between the main program and the plug-in developer. A convention
Main program code:
// When the form is loaded
Private void Form1_Load (object sender, EventArgs e)
{
// 1. Search all. dll files in the dll Directory when the form is loaded.
// 1.1 obtain the absolute path of the currently running exe
// Assembly. GetExecutingAssembly (). Location;
// 1.2 obtain the directory of the currently running exe file
String exeDirPath = Path. GetDirectoryName (Assembly. GetExecutingAssembly (). Location );
// 1.3 concatenate the absolute path of the dll file
String dllFullPath = Path. Combine (exeDirPath, "dll ");
// 1.4 search all dll files in the specified directory
String [] dellPaths = Directory. GetFiles (dllFullPath, "*. dll ");
// 1.5 cyclically traverse and load all dll files
For (int I = 0; I <dellPaths. Length; I ++)
{
// Load every dll
Assembly asm = Assembly. LoadFile (dellPaths [I]);
// Obtain the public class in the current dll (plug-in)
Type [] typePublic = asm. GetExportedTypes ();
// Obtain the interface type
Type typeIExecute = typeof (IExecute );
// Cyclically determine whether each class in typePublic implements the IExecute Interface
// Cyclically determine whether the type can be instantiated and not abstract
For (int j = 0; j <typePublic. Length; j ++)
{
If (typeIExecute. IsAssignableFrom (typePublic [j]) &! TypePublic [j]. IsAbstract)
{
// Create an object based on the type
// Because we already know that typePublic [j] implements the IExecute Interface
// Convert it to an IExecute interface and use the interface variables to operate the class members.
IExecute execut = (IExecute) Activator. CreateInstance (typePublic [j]);
// Assign a value to the name of the menu bar in the main program
ToolStripItem tsoitem = MymenuStrip. Items. Add (execut. ExeName );
// Click an event for the added function implementation
Tsoitem. Click + = new EventHandler (tsoitem_Click );
// Put the interface object execut In the tag attribute of the newly added object
Tsoitem. Tag = execut;
}
}
}
}
/// <Summary>
/// Click an event for the added function implementation
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Void tsoitem_Click (object sender, EventArgs e)
{
// Convert to the interface type
IExecute execut = (IExecute) (ToolStripItem) sender). Tag;
Execut. Execute (textBox1 );
}
Interface defined:
Public interface IExecute
{
/// <Summary>
/// The plug-in name is used to display it in the main program
/// </Summary>
String ExeName
{
Get;
Set;
}
/// <Summary>
/// Method for executing the plug-in
/// </Summary>
Void Execute (TextBox text );
}
In fact, our programmers apply reflection every day.
We are using. the reflection is applied to us. it will reflect the metadata of the current program and display all the methods, classes and other information for programmers to use, greatly improving programming efficiency!