C# reflection, calling methods by class name, method name

  In C# code, sometimes you only know the name of the method (string), and you need to call the method, then you need to use the reflection mechanism of C#. Below is a simple demo.

1  using System;
 2  using System.Reflection;
 3  
4  class Test
 5  {
 6      // No parameter, no return value method 
7      public  void Method()
 8      {
 9          Console.WriteLine( " Method (no parameter) call succeeded! " ) ;
 10      }
 11  
12      // Method with parameters and no return value 
13      public  void Method( string str)
 14      {
 15          Console.WriteLine( " Method (with parameters) The call is successful! Parameters:" + str);
 16      }
 17  
18      // Method with parameters and return value 
19      public  string Method( string str1, string str2)
 20      {
 21          Console.WriteLine( " Method (with parameters and return value) The call is successful! Parameters : " + str1 + " , " + str2);
 22          string className = this .GetType().FullName;          // Non-static method to get class name 
23          return className;
 24      }
 25  }
 26 
27  class Program
 28  {
 29      static  void Main( string [] args)
 30      {
 31          string strClass = " Test " ;            // namespace + class name 
32          string strMethod = " Method " ;         // method name 
33  
34          Type type;                           / / Storage class 
35          Object obj;                          // Instance of storage class 
36  
37         type = Type.GetType(strClass);       // Get the class of the same name through the class name 
38          obj = System.Activator.CreateInstance(type);        // Create an instance 
39  
40          MethodInfo method = type.GetMethod(strMethod, new Type[] {} );       // Get method information 
41          object [] parameters = null ;
 42          method.Invoke(obj, parameters);                            // Call the method, the parameters are empty
 43  
44          // Note that to obtain overloaded methods, you need to specify the parameter type 
45          method = type.GetMethod(strMethod, new Type[] { typeof (string ) });       // Get method information 
46          parameters = new  object [] { " hello " };
 47          method.Invoke(obj, parameters);                              // Invoke method with parameters 
48  
49          method = type.GetMethod(strMethod, new Type[] { typeof ( string ), typeof ( string ) });       // Get method information 
50          parameters = new  object [] { " hello " , "Hello " };
 51          string result = ( string )method.Invoke(obj, parameters);      // Call the method with parameters and return value 
52          Console.WriteLine( " Method return value: " + result);                 // Output Return value
 53  
54          // Get the static method class name 
55          string className = MethodBase.GetCurrentMethod().ReflectedType.FullName;
 56          Console.WriteLine( " Current static method class name: " + className);
 57  
58          Console.ReadKey();
 59      }
 60}

 

 

  It should be noted that the class name is the namespace + class name , otherwise the class will not be found.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325558431&siteId=291194637