Visual Basic .NET dynamic loading class (2)

xiaoxiao2021-03-06  40

Visual Basic .NET dynamic Load class (2) Microsoft Step 3: Creating a profile to store some of the information available to the available Form Application In runtime, you may not be available when compiling, which is usually placed in the configuration file. In Visual Basic 6.0, the configuration file should be an INI file or a Windows registry. In .NET, use XML-based profiles. We can't introduce profiles in detail because this topic is very complicated. However, you should know that the profile of the Windows Form Application is in the same directory with the application's EXE boot file. The name of the configuration file is the same as the name of the program's EXE startup file, but the suffix is ​​added after the EXE file name is added. Infig. That is to say, if you execute myApp.exe programs, the name of the configuration file must be myApp.exe.config, and the configuration file must be in the same directory with myApp.exe. The following is the configuration file to use in the example:

This This, tag is occupying information, which is convenient for people to see format. Later, we will return it back and add configuration information for the created new form. This is actually an ideal way to store form configuration information, as we use symbolic separation to save the DLL location and type name in the same location in the same location. However, the use of advanced methods to store this information, respectively, require considerable annotations and code, so we use this alternative to this. Use some text editor or XML editor (or Microsoft Visual Studio®) to create the above configuration file, then use the Formsonthefly.exe.config file name to save it in the / bin directory of the Formsonthefly project. Because the .NET configuration class uses case-sensitive XML tags, when you create this file, please pay attention to the case in the XML tag. Step 4: Read the configuration information in the collection We write the code to the form will use the class in the System.Configuration and System.Reflection namespace. Place the following two lines of code at the top of the Form1 code to make it easier to access these classes: Imports System.configuration Imports System.Reflection also requires a module-level variable to store configuration information collection. Please close the following code line next to the inherits system.windows.forms.form code line below: DIM ColavailableClasses as ArrayList, you can write core code. Place the following code in the Form1 Form Load event to read the configuration file, create a collection of objects of storage, and bind the collection data to the combo box:

'Instantiate the configuration information collection. ColavailableClasses = New ArrayList () Gets the available items to be loaded from the configuration file. Dim ClassConfigValues ​​As Specialized.NameValueCollectionClassConfigValues ​​= CType (ConfigurationSettings.GetConfig ( "availableclasses"), _ Specialized.NameValueCollection) Dim iIndex As IntegerDim sLocation As StringDim sDescription As StringDim sType As StringDim sValue As String 'may be bound to create a combo box available items 'Collection. For IIndex = 0 to classconfigvalues.count - 1sdescription = classconfigvalues.keys (iindex) Svalue = classconfigvalues.Item (SDESCRIPTION) 'Get position and type from a field from a field. Dim iPos As IntegeriPos = InStr (sValue, "~") sLocation = Microsoft.VisualBasic.Left (sValue, iPos - 1) sType = Microsoft.VisualBasic.Right (sValue, Len (sValue) - iPos) Dim objNewForm As New DynamicClass ( Slocation, SDEScription, Stype ColavailableClasses.Add (Objnewform) Next 'Now binds the collection to the combo box. 'Display instructions and return to the object's reference. CBOFORMS.DASSOSSCBOFORMS.DISPLAYCLASSCBOFORMS.DISPLAYMEMBER = "Description" CBOFORMS.VALUEMEMEMEMEMEMEMBER = "Reference" Step 5: Insert logic to load the selected window now, place the following logic in the Click event of BTnloadForm:

Dim objFormToLoad As DynamicClassobjFormToLoad = cboForms.SelectedValue Dim asmAssemblyContainingForm As [Assembly] = _ [Assembly] .LoadFrom (objFormToLoad.Location) Dim TypeToLoad As Type = asmAssemblyContainingForm.GetType (objFormToLoad.Type) Dim GenericInstance As ObjectGenericInstance = Activator.CreateInstance (TypeToLoad) DIM FORMTOSHOW AS form = ctype (genericinstance, form) formtoshow.mdiparent = meFormToShow.show () This is the core part of the program. Instantiate the code and display the form using an information of an object in a collection. Let us explain this code by line. First we reference the ObjformToload that contains the location and type of the form to load the form. It is set to the SELECTEDVALUE attribute of the combo box, and is used when returning the selected content from the combo box binding. The location of the DLL is included in the location attribute of the object. ASSEMBLY LoadForm method uses this property to create a reference to the assembly. (Plan with the Assembly class is because Assembly is .NET keyword. Brackets will notify the compiler, where the content is not the keyword being used, but the class name.) Below, we need to reference the .NET type that is being loaded. (class). You can use the GetType method of the assembly, by passing the storage type name (which will be referenced from the string of the Type property of the object of the configuration data). Save the type of reference in Typetoload. The REFLECTION class and the Activator class use their CreateInstance method to create an instance of the type. (CreateInstance is similar to CreateObject in Visual Basic 6.0.) However, an instance must be a type object because the type is to be dynamically loaded. Finally, the newly instantiated object (actually a form) must be converted to the correct type to enable the previous binding. We know that it is a form, so you can convert it to a form using the CType function. Finally, the new form is set to the sub-form of the MDI parent form and display it. Note: from

The assembly loaded by the URL shown in Death of the Browser® (English) is copied to the local cache. The assembly loaded from the UNC (such as the assembly in this article) is only used in the current location and is not copied to any cache.

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

New Post(0)