C # reflection mechanism call Method

Source: Internet
Author: User

. Net reflection (reflection) is very comprehensive and powerful, such as famous. net decompilation tool red gate's. net reflector is used. net reflection mechanism. Here is a simple instance (using the ConsoleProgram) To see how reflection is used in. net.

 
 
  1. Using system;
  2. Using system. reflection;
  3.  
  4. Namespace mengliao. CSHARP. c13.s02
  5. {
  6. Class myclass
  7. {
  8. Private int count;
  9.  
  10. Public myclass (INT value)
  11. {
  12. Count = value;
  13. }
  14.  
  15. Public void M1 ()
  16. {
  17. Console. writeline ("called method 1 .");
  18. }
  19.  
  20. Public static int m2 (int x)
  21. {
  22. Return x * X;
  23. }
  24.  
  25. Public void m3 (int x, Double Y)
  26. {
  27. Console. writeline ("called method 3, paramaters: x = {0}, y = {1: e}.", x, y );
  28. }
  29.  
  30. Public void M4 ()
  31. {
  32. Console. writeline ("called Method 4. Count = {0}", count );
  33. }
  34.  
  35. Private Static string M5 (Double X) // Private Static method, which cannot be called directly, but can be bound to a delegate
  36. {
  37. Return math. SQRT (x). tostring ();
  38. }
  39. }
  40.  
  41. Class Program
  42. {
  43. Public static void main ()
  44. {
  45. // Obtain the type object of myclass.CodeTo use the static method of type, you must specify the Assembly for the same purpose.
  46. // Type T = type. GetType ("mengliao. CSHARP. c13.s02. myclass, consoleapplication, version = 1.0.0.0, culture = neutral, publickeytoken = NULL ");
  47. Type T = typeof (myclass );
  48. // Instantiate myclass through activator for instance method call
  49. Object OBJ = activator. createinstance (T, new object [] {88 });
  50.  
  51. Methodinfo [] Methods = T. getmethods (); // retrieve the list of all methods of myclass
  52.  
  53. Foreach (methodinfo nextmethod in methods) // enumerate all methods
  54. {
  55. Console. writeline (nextmethod. tostring (); // display method information
  56. If (nextmethod. Name = "M1") // method M1
  57. {
  58. Nextmethod. Invoke (OBJ, null); // call method M1 using the OBJ object, no Parameter
  59. }
  60. If (nextmethod. Name = "m2") // method m2
  61. {
  62. // Static method, call method m2 using NULL, create a parameter array, and input 10
  63. Console. writeline ("called static method 2, return {0}", nextmethod. Invoke (null, new object [] {10 }));
  64. }
  65. }
  66.  
  67. Methodinfo m3info = T. getmethod ("m3"); // obtain method m3
  68. M3info. Invoke (OBJ, new object [] {123, 0.456}); // call method m3 and input the corresponding two parameters
  69.  
  70. // Obtain method M4. Call the method using the OBJ object without parameters.
  71. T. invokemember ("M4", bindingflags. invokemethod, null, OBJ, null );
  72.  
  73. // Create a generic delegate runme and bind the static private method M5 of myclass
  74. Delegate runme = delegate. createdelegate (typeof (func <double, string>), T, "M5 ");
  75. Console. writeline ("Call delegate with M5: SQRT (2) = {0}", (func <double, string>) runme) (2); // call this delegate
  76. Console. Readline ();
  77. }
  78. }
  79. }

The reflection mechanism can be used to call various methods, including private and static methods. The comments in the program are very detailed and I will not talk about them here.

This article from the "mengliao software Studio" blog, please be sure to keep this source http://mengliao.blog.51cto.com/876134/511804

 

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.