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.