Assemblies contain modules, and modules contain types, and types also contain members. Reflection provides an object that encapsulates assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get a type from an existing object. You can then invoke a method of the type or access its fields and properties.
We'll show you how to load a functional module by not using reflection and using reflection. Implementation results:
1. WinForm Main program
The main program we use WinForm Program, VS2008 tools C # language development. Includes toolbars, status bars, and TabControl controls. We use TabControl as a function module container to load each function module. The implementation results are as follows:
2. Do not use the reflection mode to load the function
Added information material function module project, Project Add Data Management (uccustomerinfo) user control. Data management display, load, import, export data functions.
2.1. WinForm Main program Call
WinForm Add a Data management project reference. When you click on the information material, the instance data management function module is loaded.
3. Load function using reflection mode
Using the reflection mode to load the function, you need to define the function module interface, design the configuration function module, the main program read the configuration file dynamic load function module.
3.1. Define the function module interface
The Function Module interface (UCMODULE) is defined as the user control (UserControl), the virtual two methods, for the specific function module to rewrite.
3.2. Implement the Function module instance
The function module is also the former information management function, but does not inherit the user control (UserControl), but inherits the function module interface (ucmodule).
3.3. Design Function Module Configuration
The function module configuration is defined by an XML file defined as: module number, module name, module project, module object.
3.4. By configuring the Dynamic function module
Read the function module configuration and return a list of system-defined functional modules. Add the toolbar button, and when you click the toolbar button, load the configured function module project.
At this point, how to dynamically load the function module by reflection has been explained. From the way the two examples are implemented, it is clear that dynamic loading is much more complex than the way it is used, so it is better to not use reflection in small projects, with few team members.
How to implement dynamic function module loading via reflection