Dynamic component plugging
Before implementing dynamic component plugging, we must understand a concept: reflection.
The so-called reflection is based on the name to create an object.
For example, in the above example, I can give a string directly: "csharpclassnamespace.csharprect", then I can generate this object, and call its Area () method to ask for an area! Its prerequisites are that the program can access the DLL file that stores these classes.
Ok, you can now talk about our ideas:
We put the DLL of a program and the class names in a configuration file. When the program is started, the external specified DLL can be automatically loaded, and the program's components are dynamically assembled. When I need to change a component in the program, I just need to copy the new DLL to the program folder, and the system can use the new component immediately without re-compiling or modify the original code of the component caller!
Now look at how our example is!
We created two VB class libraries, and each class library has a form. The following form is encapsulated in the VBDynamicComponent component:
Figure 6
The following form is encapsulated in the VbdyNamicComponent2 component:
Figure 7
The main program is a Windows application that dynamically creates any of the form objects in two components and displays them:
Figure 8 Sample Program Main Form
The main program reads a system configuration list when starting, is the XML format, as shown below:
XML Version = "1.0" encoding = "GB2312"?>
Config>
The main program is loaded into the specified component DLL file according to the list, and the object is created in accordance with the ComponentName property value, which is arranged automatically in the first bit.
Let's take a look, such a system component assembly list, like a chef preparation menu? Now start making our "Man Han Full"!