C # Implement the mechanism of dynamic flexibility calling business method

xiaoxiao2021-03-06  50

Original: http://www.cnblogs.com/windsails/archive/2004/09/07/40574.aspx

C # Implement the mechanism of dynamic flexibility calling business method

Problem: It is often encountered in some applications, such as recently encountering an application to roughly need to do something: 1. There are several similar services, but each processing method is different; 2. Requires processing of these services, such as a certain time may need to handle the service, and you need to handle the B service at a certain time; fixed;

Analysis of the problem:

My thoughts are to fix these business specifications for some class libraries, fixed specifications. These DLLs are then unified in a directory, and the DLL in the directory can take a simple XCopy mode to delete, and need to ensure that the main program does not occupy a DLL resource for a long time, and release it.

Solution experiment:

First, the main technical key points are first, the key is to establish a mechanism for a method in the DLL that can be dynamically invoked, so make some simple tests, and clear the establishment of this mechanism.

Here is this simple mechanism: three simple libraries, Adll, BDLL, CDLL first. The main memory program is TestReaddll, which requires the MakeStr in all DLLs below the directory DLLS, without output error messages continue to perform the next DLL. Among them, both ADLL and BDLL have similar interfaces (there is no abstraction of interfaces), but CDLL is in order to test, and similar interfaces are not implemented. Assuming that ADLL and BDLL implement method MakeStr, CDLL does not implement this method.

Due to the powerful reflex mechanism of .NET, it is not difficult to implement these functions.

Call the code of the DLL specified method in the main program:

Private

Void

Btninvoke_click

Object

Sender, System.EventArgs E)

{LStValue.Items.clear (); DirectoryInfo D = New DirectoryInfo ("DLLS"); // Directory and Current EXE in the same directory FOREACH (FileInfo File In D.GetFiles ("*. DLL") {String Pathname = D.FullName "/" file.name; // Get assembly information based on the file name Assembly.LoadFrom (Pathname); LstValue.Items.Insert (0, Assembly.Fullname "call results:" ); try {foreach (Type t in assembly.GetTypes ()) {Object obj = t.InvokeMember (null, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null); string s = (string) T. InvokeMember ("MakeStr", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.instance, Null, Obj, Null; LStValue.Items.ins ERT (0, s);}}} Catch (Exception EX) {LStValue.Items.Insert (0, EX.TOSTRING ());}}} ADLL Related Code:

Using

System;

Namespace

ADLL

{///// Class1 summary description. /// public class aclass {public aclass () {// // Todo: Add constructor logic //} public string makeStr () {string ret 4tr; RetStr = "Adll Make String"; return rettstr;}} }

BDLL related code:

Using

System;

Namespace

BDLL

{///// Class1 summary description. /// public class bclass {public bclass () {// // Todo: Add constructor logic //} public string makeStr () {string ret 4tr; retstr = "bdll make stringmodifyadd..OK2"; Return Retstr ;}}} CDLL related code:

Using

System;

Namespace

Cdll

{///// Class1 summary description. /// public class cclass {public cclass () {// // TODO: Add constructor logic //} public string makeStr2 () {string ret 4tr; RetStr = "cdll make stringmodify"; return rettstr;}} }

Mechanism test:

1. First compile ADLL, BDLL By placing it under DLLS, running TestReaddll call MakeStr results as follows:

2. Do not exit TestReadDLL, delete ADLL, run TestReaddll call MakeStr results as follows:

3. Do not exit TestReadDLL, add ADLL and CDLL to DLLS, run TestReaddll call MakeStr results as follows:

4. What is the addition of ADLL and BDLL add a copy?

You don't need to exit TestReaddll, add a copy for ADLL and BDLL in DLLS as shown below:

Running TestReaddll call MakeStr results as follows:

summary:

It can be seen that such a mechanism is very flexible, as long as the business-related DLL is placed in this directory.

In addition, the result is the same for the copy of the copy, of course, these can be done in the program to avoid duplicate calls.

The order and time rules that are called can also be defined in the program or to limit them with a configuration file.

Download the related code here.

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

New Post(0)