Practice:
Create a VB. NET Windows application: VBTestDynamicComponent. Add the ComponentList and LoadComponent classes described above to the project.
When the program starts, we read the relevant assembly information and class name information from the configuration file. To this end, add the following code in the Form_Load process of the main form:
01 Private objLoadComponent As LoadComponent
02 Private VBObj As Object
03 Private ComponentReader As MyComponentList
04
05 Private Sub VBTestDynamicForm_Load (......) Handles MyBase. Load
06 objLoadComponent = New LoadComponent ()
07 ComponentReader = New MyComponentList ("Componentlist. xml ")
08 Dim filename As String
09 Dim classname As String
10 ComponentReader. beginRead ()
11 filename = ComponentReader. getFileName (0)
12 classname = ComponentReader. getClassName (0)
13 Dim ret As Boolean
14 ret = objLoadComponent. LoadComponentLibrary (filename)
15 VBObj = objLoadComponent. LoadClass (classname)
16 VBObj. show ()
17 End Sub
We can see that the DLL file name and class name are extracted from the configuration file in the 07--10 sentence, the first DLL file name and class name are obtained in the 07--10 sentence, and then the component library is loaded in the 07--10 sentence, the 15th statement loads the object. Because the returned object is actually a form, you can directly call the Show () method in the 16th statement to display the form.
The code shows how convenient and simple it is to dynamically create objects in. NET! Note: in the project, we do not even need to add references to specific components to the project! In this example, the dynamically created object is a Form, and the Form object can be used by default. In a real project, all interfaces can be put into a specific DLL, which is called "Facade Assembly". Other projects only need to reference this DLL, all components implementing these interfaces can be freely created by name!
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