When writing a program, we often encounter the following situation: a certain type of computing is used in the program, and there are many such computing methods, so we have to fully consider it when writing the program, consider various situations. However, this is very laborious, because we cannot predict what kind of computing method will appear after the program is compiled. If the calculation method is delivered to the customer, we have to write the new calculation method into the program, re-compile it, and then deliver it to the customer. It is quite troublesome to do this, and it seems costly to re-compile the entire project just for such a short program.
Using some methods in System. Reflection in MS. NET can help us solve the above problems.
First, when we encounter the problem above, we need to analyze it first. What parameters does this calculation require? What are their common parameters in different calculation methods? Can special parameters in different calculation methods be calculated by common parameters or by other methods? After analysis, extract the available common parameters.
Next, we can compile the computing method. Every method of this computation is written as a method in a DLL class and compiled into a DLL file. In MS. NET, the format of the class must be fixed. That is to say, the namespace of the written class must be the same as that of the class, and the method name in the class must be the same. In addition, the parameters of the method are the common parameters mentioned above.
Then, place the compiled DLL file in the same folder and release it with the program.
In the program, we can use different computing methods as follows: Give each computing method a name (which the customer can understand ), put these names in the text attribute of the drop-down list box, and put the corresponding DLL file name in the value Attribute of the drop-down list box. In this way, you can call the calculation methods in different DLL files by selecting different calculation methods.
The following is a simple example:
I put the computing method name and DLL file name in an XML file, and read them to the drop-down list box during program loading. The method is as follows: (IN Asp.net)
// Place user code here to initialize the page
If (page. ispostback = false)
{
// Read the XML file when the page is loaded for the first time
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 relevant content of the read XML file to the drop-down list box.
For (I = 0; I <xmlnd. selectnodes ("field"). 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 entering the parameters, you can use the following code to complete the calculation and display the results. The write method is to return a DataTable after calculation.
DataTable dt = null;
// Load the dll file of the class
Assembly ass = Assembly. LoadFrom (Server. MapPath (this. ddlType. SelectedItem. Value ));
// Obtain the type
Type tp = ass. GetType ("MyNamespace. MyClass ");
// Obtain Method
MethodInfo mi = tp. GetMethod ("MyMethodl ");
// Create an instance
Object obj = System. Activator. CreateInstance (tp );
// Array of pay-as-you-go parameters (type consistency required)
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 (obj, objArray );
// Bind to the DataGrid if a return value exists.
If (dt! = Null)
{
This. grdResult. DataSource = dt;
This. grdResult. DataBind ();
}