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 );