Visual Basic .NET dynamic loading class (3)

xiaoxiao2021-03-06  44

Visual Basic .NET dynamidity loading class (3) ·· Microsoft Step 8: Update the configuration file with new form information now, we have created some new forms and reference them in the configuration file. Use the following code line to replace the placeholder information in Formsonthefly.exe.config:

If you want to change the location or name of the form, you need to The above code line is modified accordingly. Step 9: Run Formsonthefly.exe and load the form Now perform Formsonthefly.exe (no Visual Studio). Select a form in the combo box and click BtnLoadForm. If all steps are completed correctly, you can see that the child form is loaded into the MDI window (even if the child form does not exist even if the MDI application is compiled). Figure 2 shows the MDI form after the dynamically loaded form. Figure 2: MDI form after dynamically loading the form At this time, you can create any multiple new forms as needed and load it into the MDI application. To make it available, compile it into a class library and add a reference to them in the configuration file. Dynamic Loading Other Types This technique can be used in other types of classes in addition to the form available in the form. However, the situation is relatively complicated. In the above example, the reason why the system.windows.forms.form class interface is performed because we know that it is a form. Therefore, a form of a form can be used (eg, a Show method). But for the classes you created yourself, can you bind it according to what interface? The answer is to create our own interface. In fact, this is an important example of an important implementation interface. You may use an interface in Visual Basic 6.0. The interface is created as an empty class. There is a new syntax in Visual Basic .NET, the interface is completely separated, and a separate statement is made. In step 1 below, we will see how to achieve this. Let us design an example that can be dynamically loaded. Suppose we have to write a data cleaning application that requires a data set. However, just like all data cleaning procedures, they seem to have never been completed. It seems that you always need to write code for new check types and cleanup tasks. Of course, you can create a new cleanup logic and recompile the entire application, but if you can dynamically insert a new data cleaning function without having to recompile the primary application, is it better? Let's create such an example from the beginning. Step 1: Create a class file to store the interface From the concept, create an interface in Visual Basic .NET, create an interface in Visual Basic 6.0, but is very different in grammar. To create an initial interface class, create a new project with a Class Library (class library). Name it ScrubberInterface. In the created class file, replace the existing code line in the file with the following code:

Public Interface IscrubbBersub Scrub (Byval DS AS Dataset) End Interface Our interface is very simple. We need a method of operating a data set. This method has been named SCRUB. Create a project now. ScrubberInterface.dll will be created under the / bin directory of the project. Step 2: Creating a Cleanup Application Cleanup Application and the previous form application example is similar in many ways. Different, we will use all the data cleaning classes in turn, rather than choosing a specific class. Create a new Windows Form Application and name it Classesonthefly. The following controls are placed in the display Form1: Control Name Type property ButtonbtnLoadDatasetText = "Load Dataset" ButtonbtnScrubDatasetText = "Scrub Dataset" DataGridDataGrid1 Form1 without changes on the code, the code placed in the top of the previous example the same code: Imports System.Configuration Imports System.Reflection also requires a modular-level variable to store configuration information collection. Place the following code line below the inherits system.windows.forms.form code line: DIM COLAVAILASSESS AS ARRAYLIST Step 3: Load the Data Set to the grid to simplify the operation, load the data set from the XML file. Place the following XML file into the / bin directory of the ClassSonthefly project, and then name it TimecardData.xml:

sherlock holmes 123 John Watson 345 345 iRene Adler 567 jabez Wilson 789 < / TIMECARDDATA> We need a module-level dataset reference, so put the following code line below the code line of declaration ColavailableClasses: DIM DS AS DataseT To read the data set and load the grid, place the following code to btnloadDataSet In the Click event:

DS = New DataSet () DS.ReadXML ("TimecardData.xml") DataGrid1.datasource = DS.TABLES (0) Then run the program to ensure proper load. Step 4: Add the reference to the cleaning program interface, we need to add a reference to the DLL, which is previously created, used to store the interface of the data cleaning class. Select Project | Add Reference (Project | Add Reference). Click the Browse button to browse to Scrubberinterface.dll (create in step 1), then click Open. Click OK to add a reference. Step 5: Allows the class to store data on the available form Now, copy the DynamicClass.vb module used in the previous form example, and insert it into the ClasseSonthefly project. You can use it directly without modification. Step 6: Create a configuration file to store available forms to create a profile as the profile used in the previous form example. The configuration file should be displayed as follows:

Keep in mind that this file is placed ClassesOnTheFly project / bin directory In, then name it to classesonthefly.exe.config. Step 7: This operation is almost identical to the operation in the previous form example, and the set is not bound to the combo box now. Therefore, replicating the logic used in the Form Load event of the form example, but omits the last three line code (this three line code is used to perform the operation of the bind to the combo box). Paste this logic into the Form1's Form Load event in the CLASSONTHEFLY project. Step 8: Insert the logic to load and use the data cleaning class now, place the following logic in the Click event of the btnscribDataSet:

Dim objScrubberClass As DynamicClassFor Each objScrubberClass In colAvailableClassesDim asmAssemblyContainingForm As [Assembly] = _ [Assembly] .LoadFrom (objScrubberClass.Location) Dim TypeToLoad As Type = _asmAssemblyContainingForm.GetType (objScrubberClass.Type) Dim GenericInstance As ObjectGenericInstance = Activator.CreateInstance (TypeToLoad) Dim Scrubber as scrubberinterface.iscrubber = _ctype (genericInstance, scrubberinterface.iscrubber) Scrubber.scrub (DS) Next This logic is similar to the logic of dynamic loading forms, which is no longer described herein. The main difference is that every class in the configuration file is dynamically loaded. The newly instantiated object is converted to the Scrubberinterface.iscrubber type. This can be bound to the ISCRUBBER interface. Execute the SCRUB method of each object and transfer in the data set. Step 9: Compile and run the application to run the application to ensure proper compilation. However, don't click the Scrub Dataset button because you have not created the cleaning class. After the application is completed, close the Visual Studio in the Classesonthefly project. Step 10: Creating a Data Cleanup class in Visual Studio creates a new project with a type Class Library (class library). Name it as firstclass. Use the following code to replace the Class1 automatically inserted code:

Public Class FirstClassImplements ScrubberInterface.IScrubberPublic Sub Scrub (ByVal ds As DataSet) _Implements ScrubberInterface.IScrubber.ScrubDim dr As DataRow dr = ds.Tables (0) .NewRowdr.Item (0) = "Professor Moriarty" dr.Item (1) = "666" DS.TABLES (0) .Rows.Add (DR) End Sub End Class This class implements the ISCRUBBER interface (just a method). This method obtains a data set and adds a single row in the data set. Of course, all necessary data operation logic should have been provided in the actual data cleaning class. Create this item for firstclass.dll. Copy the DLL from the item / bin directory to a new directory named C: / ScrubberClasses. Step 11: Update the configuration file with the new class now, return to classonthefly.exe.config. Change the content in the tag, make it as follows:

Save the configuration file and perform the last step. Step 12: Test the new data cleaning class now, run classesonthefly.exe and click the Load DataSet button. Please note that the grid contains four lines. Click the Scrub DataSet button. The fifth line will be displayed in the grid (this is added by the data cleaning class). If you need, you can add additional cleaning classes to perform any of the required operations on the data set. Simply create the cleaning class and add it to the configuration file. This way, these cleaning classes will be automatically used when clicking the Scrub DataSet button. The most important point in these two examples is that when creating and compiling the original application (Formsonthefly and ClasseSonthefly), there is no reference to the subsequently loaded forms and classes. In fact, these forms and classes have not yet been created when compiling applications! After creating a form and class, you can use them to update the application using them in the configuration file. A new form and class can be created as needed and dynamically added. If your application needs to have this extension, you can use the .NET provided to improve the solution, which has the function of reflecting and dynamically loading.

转载请注明原文地址:https://www.9cbs.com/read-79060.html

New Post(0)