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:
Xml version = "1.0" encoding = "utf-8"?>
'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.