Dynamic loading class (call DLL file in the program)

zhaozj2021-02-16  49

Dynamic loading class (dynamically loaded DLL file)

I have just started writing procedures, and I found a very good way in the development. Everyone shares.

We often encounter such a situation when writing programs: there is a lot of calculations in the program, and this calculation method has a lot of calculation, we have to consider it very comprehensive when writing a program, will considering. But this is also very laborious, because we can't predict how the program is compiled, what kind of calculation method will also occur. If the calculation method is to deliver to the customer, our newly proposed us have to write new computing methods, then recompile, and then hand it over. This is quite troubles, and only for such a short program, it is necessary to recompile the entire project. It seems that the cost is quite big. Use some of the method in System.Reflection in MS.NET to help us solve the above problems.

First, when we encounter the above problems, we must first analyze it. What parameters do you need? What is the common parameters in different computing methods? Is the unique parameter in different computing modes can be calculated by a common parameter, or obtained by other methods? After the analysis is finished, the common parameters available are extracted. Next, we can write a calculation method. Each of this calculation is written into a method in a DLL in a class and compiles it into a DLL file. In MS.NET, the format of the class is to be killed, that is, the names of the Namespace and Class written, the method name in the class must also be the same. Moreover, the parameters of the method are the common parameters mentioned above. Then, put the compiled DLL file in the same folder, and it can be published along with the program.

In the program, the different calculation methods to be used should be handled: give each calculation mode (the customer can understand), then put these names in the text property of the drop-down list box, and will correspond to the corresponding DLL file The name is placed in the Value property of the drop-down list box. In this way, the user selects a different calculation method to call the calculation method in different DLL files.

Here is a simple example: I put the name of the calculated method and the DLL file name in an XML file, and the program is loaded to the drop-down list box. The method is as follows: (ASP.NET) / / The user code is placed here to initialize the page if (page.ispostback == false) {// page first loads the XML file system.xml.xmldocument Xmldoc = New System. Xml.xmldocument (); system.xml.xmlnode Xmlnd; INT i;

XMLDoc.Load (Server.MAppath ("MyConfig.xml"); XMLnd = XmLDoc.selectsinglenode ("// DLLFILE");

// Write the contents of the read XML file to the drop-down list box in the drop-down list box. Count; I ) {ListItem it = new ListItem (); it.text = Xmlnd.selectNodes ("Field"). Item (i) .attributes ["text"]. value; it.value = xmlnd.selectNodes ("Field"). Item (i) .attributes ["filename"]. Value This.ddltype.Items.add (it);}} Then, after the parameter is entered, the following code can be used to complete the calculation and display the result. The way I wrote is to return a DataTable after calculating.

DataTable DT = NULL;

/ / Load the DLL file assembly assemble assemblyMbly Ass = askPMBLY.LOADFROM (server.mappath (this.ddltype.selected); // Get Type Type TP = Ass.gettype ("MyNameSpace.myClass"); // Get Method methodIndinfo mi = tp.getMethod ("mymethodl"); // Create an instance object obj = system.activator.createInstance (TP);

// Pay value parameter array (requirement type consistency) Object [] objarray = new object [7]; objarray [0] = convert.todouble (this.textBox1.text); objarray [1] = Convert.Toint32 (this. TextBox2.text); objarray [2] = convert.todouble (this.textBox3.text); // Call method Dt = (DataTable) mi.invoke (objarray);

// Bind to DataGrid if (dt! = Null) {this.grdRDRDRESULT.DATABIND ();}

Author: shade (yistudio) E-mail: yistudio@hotmail.com

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

New Post(0)