Based on the component. NET software Development (4)

Source: Internet
Author: User
Tags integer reflection
Components used by the design sample
Create two VB.net class library projects: Dynamiccomponent and VBDynamicComponent2, create two forms VBForm1 and VBForm2 (shown in Figure 6, Figure 7), the former in the Dynamiccomponent project, the latter in the Vbdynami The CComponent2 project.

Compile and generate two DLL files separately: DynamicComponent.dll and VBDynamicComponent2.dll.

Next, we create a Windows application Vbtestdynamiccomponent to test our Assembly technology.

Reading XML configuration Files
When the test program starts, it reads the information from the XML configuration file, and we see that the relevant information can be divided into two categories: one is the list of DLL files to load, the other is the class name that needs to be loaded. The two are one by one corresponding, so we can create two ArrayList, one for storing the filename, one for storing the class name, and then for encapsulating the two ArrayList with a class mycomponentlist, the external user simply gives the index, You can get both the file name and the class name immediately.

The interface of the class is designed as follows:





Figure 9 Class for implementing a dynamic loading component



See Figure 9, as long as you specify an XML configuration file name for the object of the Mycomponentlist class, and then call BeginRead (), the caller can pass the index (0,1,2 ...). To get the file name and class name.

Reading XML format data can use the XmlTextReader classes provided by the. NET Framework. The complete code is as follows:



' Read the component's class name and filename from the XML configuration file

Imports System.Collections.Specialized

Imports System.Windows.Forms

Public Class Mycomponentlist

Private XmlReader as Xml.xmltextreader

Private _filename as String ' XML configuration Name

Private _componentfilename as String ' component library filename

Class name in Private _componentname as String ' component library

Private Componentnames as ArrayList ' holds all component class names listed in the configuration file

Private Componentfiles as ArrayList ' holds all the component library names listed in the configuration file



' Create related objects in constructors

Public Sub New (ByVal FileName as String)

_filename = FileName

_componentfilename = ""

_componentname = ""

Componentnames = New ArrayList ()

Componentfiles = New ArrayList ()

XmlReader = New Xml.xmltextreader (FileName)

End Sub



' XML configuration file name

Public Property FileName () as String

Get

Return _filename

End Get

Set (ByVal Value as String)

' The file name should throw an exception.

_filename = Value

End Set

End Property

' Read Component library

Public Sub BeginRead ()

Dim B1, B2 as Boolean

B1 = False

B2 = False

' The following loops read into the file, using the tag B1 and B2 to implement the pairing of the component library file ßà component name.

' and deposit them into the corresponding two ArrayList (Componentfilesßàcomponentnames)

While XmlReader. Read

If XmlReader. Name = "Component" Then

XmlReader. MoveToFirstAttribute ()

If XmlReader. Name = "ComponentName" Then

_componentname = XmlReader. Value

B1 = True

End If

If XmlReader. Name = "Componentfilename" Then

_componentfilename = XmlReader. Value

B2 = True

End If

While XmlReader. MoveToNextAttribute ()

If XmlReader. Name = "ComponentName" Then

_componentname = XmlReader. Value ()

B1 = True

End If

If XmlReader. Name = "Componentfilename" Then

_componentfilename = XmlReader. Value ()

B2 = True

End If

If B1 and B2 Then

Componentnames.add (_componentname)

Componentfiles.add (_componentfilename)

B1 = False

B2 = False

End If

End While

End If

End While



End Sub

' Remove the filename of the specified index (that is, the component library file name)

Public Function GetFileName (ByVal index as Integer) as String

Return Componentfiles.item (Index)

End Function

' Remove the class name of the specified index (that is, the component name)

Public Function getclassname (ByVal index as Integer) as String

Return Componentnames.item (Index)

End Function

End Class



The code is very clear, and there is no more nonsense.

Creating objects Dynamically
Knowing the components that need to be loaded, the next step is how to create objects, which requires the use of classes in System.Reflection.

Similarly, we also design a class loadcomponent to encapsulate the ability to create objects:





Figure 10 completing the class that created the Component object



The use of this class is very simple, given a DLL filename, the loadcomponentlibrary () function is used to mount the DLL into memory, and then you can use LoadClass to create objects of the specified class.

Now let's step through the class.

(1) Loading component library:

First of all, it is important to understand that in. NET, the reusable component library is referred to as "Assembly", which is generally translated as "assembly" (the component library referred to above, which in fact refers to an assembly), in most cases, each. NET DLL is an assembly. A class that can be reused is placed in an assembly. To do this, to dynamically create an object based on the name of the class, you must first load the assembly into memory. In. NET, this can be achieved by reflection technology. The sample code is as follows:



Private Mydll as System.Reflection.Assembly

Public Function loadcomponentlibrary (ByVal componentfilename as String) as Boolean

' Mount the specified component code base

' Success returns True

Try

Mydll = System.Reflection.Assembly.LoadFrom (componentfilename)

If Mydll is nothing Then

MessageBox.Show ("Can ' t Load Library")

Return False

End If

Catch Errobj as SystemException

MessageBox.Show (Errobj.message)

Return False

End Try

Return True

End Function



(2) Creating objects:

Once the assembly is loaded into memory, we can create the object based on the class name string, as follows:



Private obj as Object

Public Function loadclass (ByVal classname as String) as Object

If Mydll is nothing Then

Return Nothing

End If

Try

obj = mydll.createinstance (classname)

Catch Errobj as SystemException

MessageBox.Show (Errobj.message)

Return Nothing

End Try

return obj



End Function



With the LoadComponent class, what we have to do in the future is much simpler.



Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.