Introduction to loading external DLL [go to:]

Source: Internet
Author: User

Using system;
Using system. Windows. forms;

Namespace arli. commonprj {
Public class showaboutbox {
Public static void showon (Form FM ){
MessageBox. Show ("OK ");
}
}
}

After compilation, the file name is aboutbox. dll.

The calling method in the main program is as follows:

// Define the file name
Fileinfo aboxfile = new fileinfo (path. Combine (application. startuppath, "aboutbox. dll "));

If (aboxfile. exists) {// If yes
Try {// prevent exceptions, such as incomplete loads or invalid DLL
// Start loading
Assembly abox = assembly. loadfrom (aboxfile. fullname );
Type [] _ t = abox. gettypes (); // obtain all types
Foreach (type T in _ t) {// traverse
// If the namespace and class name match
If (T. namespace ="Arli. commonprj "&&T. Name ="Showaboutbox "){
// Loading Method
Methodinfo M = T. getmethod ("showon ");
If (M! = NULL) {// if the method to be loaded exists
// Create an instance
Object o = activator. createinstance (t );
// Execute this method, followed by this parameter
M. Invoke (O, new object [] {This });
}
Else {// The loaded method does not exist
MessageBox. Show ("file/" aboutbox. dll/"invalid! /N/nmethod error .");
}
Return;
}
}
MessageBox. Show ("file/" aboutbox. dll/"invalid! /N/nassembly name error .");
} // The file, namespace, and method are consistent, but an error occurred while executing the DLL content.
Catch (system. nullreferenceexception ex ){
MessageBox. Show ("file/" aboutbox. dll/"invalid! ");
} // Abnormal DLL file
Catch (exception ex ){
MessageBox. Show ("file/" aboutbox. dll/"error:/n" + ex. Message );
}
}
Else {// file not found
MessageBox. Show ("file/" aboutbox. dll/"missing! ");
}

Note 1: If type T = GetType ("arli. Comm...") is used directly, an exception occurs if this class does not exist.
NOTE 2: This type of reverse reflection dynamically loads accessory information without being defined
NOTE 3: This method has been used to detect the potential to the greatest extent. Unless it is a non-legal Windll (such as incomplete download), it will not enter a very slow try catch.

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.