C # reflect the dynamic call of methods in DLL and return results

Source: Internet
Author: User

I recently looked at the factory development model and found that reflection was used. I only heard of and never used it. So I spent some time reviewing it again;

The function of reflection is to dynamically load a DLL (ProgramSet), execute a method in the Assembly, and return the result. Of course, you can also pass parameters to the method.

 

 Namespace  Assembly_name {  Public   Class  Assembly_class {  Public   String Show_str ( String  Str ){  If ( String . Isnullorempty (STR ))  Return   "  You have not passed in the Parameter  "  ;  Else                    Return   "  There are parameters. The parameters are:  " + STR ;}}} 

Write a test class above. The namespace is assembly_name and the class name is assembly_class. The show_str parameter in the class name is of the string type and the return value is of the string type;

This class was generated in my workshop. The generated DLL is app_code.dll (this can be based on your situation, not entirely app_code.dll ), add the DLL to your project (important );

 

 

 //  Load the Assembly (DLL file address) and use the Assembly class Assembly = assembly. LoadFile (appdomain. currentdomain. basedirectory + "  Bin/app_code.dll  "  );  //  Get type, parameter (namespace + class) Type type = assembly. GetType ( "  Assembly_name.assembly_class  "  );  // Instance for creating this object, object type, parameter (namespace + class)  Object Instance = assembly. createinstance ( "  Assembly_name.assembly_class  "  );  //  Set the parameter type in the show_str method. Type [] type. If multiple parameters exist, you can append multiple parameters. Type [] params_type = New Type [ 1  ]; Params_type [  0 ] = Type. GetType ( " System. String  "  );  //  Set the parameter values in the show_str method. If multiple parameters exist, you can append multiple parameters. Object [] params_obj = New Object [ 1  ]; Params_obj [  0 ] = "  Jiaopeng  "  ;  //  Execute the show_str Method Object Value = type. getmethod ( "  Show_str  " , Params_type). Invoke (instance, params_obj );

 

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.