How to implement dynamic function module loading and dynamic function module through reflection
The Assembly contains modules, while the module contains types and types including members. Reflection provides encapsulated assembly, module, and type objects. You can use reflection to dynamically create instances of the type, bind the type to an existing object, or obtain the type from an existing object. Then, you can call methods of the type or access its fields and attributes.
The following describes how to load function modules by Using Reflection instead of reflection. Effect:
1. WinForm main program
The main program is developed using the Winform program and VS2008 tool C. Including the toolbar, Status Bar, and TabControl controls. We use TabControl as the function module container to load each function module. The implementation result is as follows:
2. Do not use the reflection Loading Function
The information and materials module project is added, and the UCCustomerInfo user control is added to the project. Data Management displays, loads, imports, and exports data.
2.1. WinForm main program call
Add a data management project reference in WinForm. When you click "information", the instance data management module is loaded.
3. Use the reflection Loading Function
To use the reflection Loading Function, You need to define the function module interface, design the configuration function module, and the main program to read the configuration file to dynamically load the function module.
3.1. Define function module Interfaces
The Function Module Interface (UCModule) is defined as a user control (UserControl), which is a virtual two-way method for specific function modules to rewrite.
3.2. Implementation of function module instances
The function module is the former information management function, but does not inherit the user control, but inherits the function module interface (UCModule ).
3.3. design function module configuration
The function module configuration is defined in an Xml file and is defined as: module number, Module name, Module Project, and module object.
3.4. Configure the dynamic function module
Read the function module configuration and return to the list of function modules defined by the system. Add the toolbar button. When you click the toolbar button, load the configured functional module project.
Now, how to use the reflection dynamic loading function module has been explained. From the two implementation methods, we can see that the dynamic loading method is much more complicated than the direct use method, so when there are few small projects and team members, we recommend that you do not use reflection.