C # dynamically loads DLL files and implements methods that call them (recommended) analysis

Source: Internet
Author: User
The following small series for everyone to bring a C # dynamic load DLL file, and implement the method called (recommended). Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

Here is the test code:

Create a new classlibrary, containing two classes Class1 and Class2, each of the two classes has a method that returns a string with the following code:

Using system;using system.collections.generic;using system.linq;using system.text;namespace mydll{Public  class Class1  {public    Class1 ()    {    } public    string SayHello ()    {      return ' hello,word! ';    }  } Public  class Class2  {public    Class2 ()    {    } public    string saybeautiful ()    {      return "Beautiful,very good!";    }  }}

A mydll.dll dynamic-link library is generated after compilation, and a new WinForm project is created (and, optionally, debug):

private void Button1_Click (object sender, EventArgs e) {string path = @ "D:\123\mydll\mydll\bin\Debug\mydll.dll";      byte[] byte1 = System.IO.File.ReadAllBytes (path),//is also possible//assembly Assem = Assembly.Load (byte1);      Assembly Assem = assembly.loadfile (path); String t_class = "Mydll. Class1 ";//theoretically loaded DLL file, you can get the class type by namespace plus class name, here should be modified as follows://string t_class =" Mydll. Class1,mydll ";//If what you want to get is the class inside the project, you can" namespace. Parent class ...      The class name "; if it is external, it needs to be appended with the", link library name ";      Thanks again for Thy38 's help. Type ty = Type.GetType (t_class);//Here in the debugging time Ty=null, always do not understand, looking for an expert can doubt type[] Tys = Assem. GetTypes ();//had to get all the type names and then traverse through the type name to differentiate the foreach (type ty in Tys)//huoquleiming {if (Ty). Name = = "Class1") {constructorinfo magicconstructor = ty. GetConstructor (type.emptytypes);//Gets the constructor without parameters object Magicclassobject = Magicconstructor.invoke (new object[] {})         ;//Here is an east//object magicclassobject = activator.createinstance (t) to get a class-like instance, or a constructed instance with no arguments can be MethodInfo mi = ty.          GetMethod ("SayHello"); Object Aa=mi.          Invoke (magicclassobject, NULL); MessageBox.Show (AA. ToString ());//Here is the SayHello method for executing class Class1} if (Ty. Name = = "Class2") {constructorinfo magicconstructor = ty.          GetConstructor (type.emptytypes);           Get a constructor without parameters, if there is a constructor and there is no constructor without parameters, this is not the case here. Object Magicclassobject = Magicconstructor.invoke (new object[] {}); MethodInfo mi = ty.          GetMethod ("saybeautiful"); Object AA = mi. Invoke (magicclassobject, NULL);//If the method has parameters, it needs to replace NULL with the set MessageBox.Show (AA) of the parameter.        ToString ()); }}//appdomain Plugindomain = (Plugininstancecontainer[key] as pluginentity).      Plugindomain;      if (plugindomain! = null)//{//AppDomain.Unload (Plugindomain); // }     }

The above is the C # dynamic load DLL file, and implement the method (recommended) To call the content of analysis, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.