C # Implementation of dynamic and flexible calling of Business Methods

Source: Internet
Author: User

Question:
In some applications, this situation often occurs. For example, a recent application generally needs to do the following:
1. There are several similar services, but each processing method and output are different;
2. These services need to be processed dynamically. For example, a business may need to be processed at a certain time, and B's business needs to be processed at a certain time;
3. The number of services to be processed is unknown and may increase or decrease at any time;
4. Hope the subjectProgramRelatively fixed;

Problem Analysis:
My idea is to standardize these services into some class libraries and fix standard interfaces. Then place these DLL files under a directory. The DLL files in the directory can be added and deleted in a simple xcopy mode. In addition, ensure that the main program does not occupy a DLL resource for a long time and is released after the call.
Solution experiment:
First, extract the key technical points. The key is to establish a mechanism that can dynamically call a method in the DLL under a directory. Therefore, do some simple tests first, clarify the establishment of this mechanism.

The following is a simple mechanism:
Three simple class libraries, adll, bdll, and cdll, are created first. The main caller is testreaddll. It needs to call makestr in all the DLL files under the relative directory DLLs, and no error message is output to continue executing the next DLL. Both adll and bdll have similar interfaces (the interfaces are not abstracted here), but cdll does not implement similar interfaces for testing.

Assume that adll and bdll implement the makestr method, and cdll does not implement this method.

Thanks to the powerful reflection mechanism of. net, it is not difficult to implement these functions.

The main program calls the method specified by DLLCode:

Private   Void Btninvoke_click ( Object Sender, system. eventargs E)
{
Lstvalue. Items. Clear ();
Directoryinfo d =   New Directoryinfo ( " DLLs " ); // The directory is in the same directory as the current exe.
Foreach (Fileinfo File In D. getfiles ( " *. Dll " ))
{
String Pathname = D. fullname +   " \ "   + File. Name;
// Obtain assembly information based on file name
Assembly = Assembly. loadfrom (pathname );
Lstvalue. Items. insert ( 0 , Assembly. fullname +   " 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. insert ( 0 , S );

}
}  
Catch (Exception ex)
{
Lstvalue. Items. insert (0, Ex. tostring ());
}
}

}

Adll code: Using System;

Namespace Adll
{
/// <Summary>
///Class1.
/// </Summary>
Public   Class Aclass
{
Public Aclass ()
{
//
//Todo: add the constructor logic here
//
}

Public   String Makestr ()
{
StringRetstr;
Retstr= "Adll make string";
ReturnRetstr;
}
}
}

Bdll code: Using System;

Namespace Bdll
{
/// <Summary>
///Class1.
/// </Summary>
Public   Class Bclass
{
Public Bclass ()
{
//
//Todo: add the constructor logic here
//
}

Public   String Makestr ()
{
StringRetstr;
Retstr= "Bdll make stringmodifyadd .. ok2";
ReturnRetstr;
}

}
}

Cdll code:Using System;

Namespace Cdll
{
/// <Summary>
///Class1.
/// </Summary>
Public   Class CClass
{
Public CClass ()
{
//
//Todo: add the constructor logic here
//
}

Public   String Makestr2 ()
{
StringRetstr;
Retstr= "Cdll make stringmodify";
ReturnRetstr;
}

}
}

Mechanism test:
1. Compile adll and bdll and put it under DLLs. Run testreaddll and call makestr. The result is as follows:

2. You do not need to exit testreaddll and delete the adll. Run testreaddll and call makestr. The result is as follows:

3. Do not exit testreaddll. Put adll and cdll into DLLs again. Run testreaddll and call makestr. The result is as follows:

4. What if adll and bdll Add a copy?
You do not need to exit testreaddll and add a copy for adll and bdll In the DLLs such:

Run testreaddll and call makestr. The result is as follows:

Summary:
From this we can see that this mechanism is very flexible, as long as you put the service-related DLL under this directory.
In addition, the call results for copies are the same. Of course, these restrictions can be imposed in the program to avoid repeated calls.
The call sequence and time rules can also be defined in the program or restricted by a configuration file.
Download the relevant code here.
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.