Common usage of reflection

zhaozj2021-02-16  86

1. reflection to invoke the form private void button1_Click (object sender, System.EventArgs e) {Assembly assm = Assembly.LoadFrom ( "e: //WindowsApplication5.dll"); Type TypeToLoad = assm.GetType ( "WindowsApplication5.Form1") ; object obj; obj = Activator.CreateInstance (TypeToLoad); Form formToShow = null; formToShow = (Form) obj; formToShow.Show ();} 2private void button1_Click (object sender, System.EventArgs e) {try {Type t = TYPE.GETTYPE ("WindowsTest.form2"); object obj = system.activator.createInstance (t); methodinfo mif = t.getMethod ("show"); mif.invoke (obj, null); // OR ((Form) ) obj) .Show ();} catch (Exception ex) {MessageBox.Show (ex.Message);}} 3private void btnReflection_Click (object sender, System.EventArgs e) {try {Assembly assembly = Assembly.LoadFrom (@ " D: /WindowsTest/WindowsTest/bin/Debug/MyClassLib.dll "); Type typeload = assembly.GetType (" MyClassLib.Class1 "); object obj = Activator.CreateInstance (typeload); // MethodInfo method = typeloa D. GetMethod ("ShowMessage", new type [] {typeof (string), typeof (int)}); //method.invoke (obj ,new object [] {"2342", 213}; methodinfo [] Methods = typeload.GetMethods (); MethodInfo method = typeload.GetMethod ( "ShowMessage", Type.EmptyTypes); method.Invoke (obj, null); MessageBox.Show ( "Success!");} catch (Exception ex) {MessageBox .Show (ex.Message);}}

7. The contents of the DLL file are as follows:

Using system; using system.windows.forms;

Namespace arli.commonprj {public class showaboutbox {public static void showon (form fm) {messagebox.show ("ok");}}}

Post-compiled file name called AboutBox.dll

The calling method in the main program is as follows: // Defines file name fileinfo aboxfile = new fileinfo (path.combine (application.startuppath, "aboutbox.dll"));

If (Aboxfile.exists) {// If there is try {// prevent accident, such as without incomplete, illegal DLL // start load assembly abox = askEMBLY.LOADFROM (Aboxfile.FullName); type [] _t = Abox. Gettypes (); // Get all Type Foreach {// Traverse // If you find name space and class name, IF (t.namespace == "arli.commonprj" && t.name == "Showaboutbox") {// load method methodinfo m = t.getMethod ("showon"); if (m! = Null) {// If the method to be loaded exists // creates an instance Object O = Activator.createInstance T); // Execute the method, the next THIS is the parameter M.Invoke (O, New Object [] {this});} else {// loaded method does not have MessageBox.show ("File /" AboutBox. DLL / "invalid! / n / nmethod error.");} return;}} messagebox.show ("file /"aboutbox.dll/" INVALID! / N / NASSEMBLY NAME ERROR. ");} // file, name The space, method is consistent, but execute the DLL content error CATCH ("file /"aboutbox.dll/" invalid! ");} // File is abnormal DLL Catch (Exception EX) {MessageBox.show ("file /"aboutbox.dll/" error: / n / n " ex.Message);}} else {// file did not find MessageBox.show (" file /"aboutbox.dll/ "Missing ! ");

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

New Post(0)