Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 9th, 2012  |  syntax: None  |  size: 2.04 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to call a Method from String using c#?
  2. class Class1
  3. {
  4.     public void method1()
  5.     {
  6.         string calltoaction = "Class2.Method2()";
  7.     }
  8. }
  9.        
  10. public Form1()
  11. {
  12.     InitializeComponent();
  13.  
  14.     Action<string> calltoaction;
  15.     calltoaction = Doit;
  16.     calltoaction("MyText1");
  17.     calltoaction = Doit2;
  18.     calltoaction("MyText2");
  19. }
  20.  
  21. void Doit(string s)
  22. { Text = s; }
  23.  
  24. void Doit2(string s)
  25. { textBox1.Text = s; }
  26.        
  27. public class Class2
  28. {
  29.     public static void Method2() { }
  30. } // eo class 2
  31.  
  32. public class Class3
  33. {
  34.     public static void Method3() { }
  35. } // eo class 3
  36.  
  37. public class Class4
  38. {
  39.     public static void Method4() { }
  40. } // eo class 4
  41.        
  42. public class MainClass
  43. {
  44.     private delegate void MethodDelegate();
  45.     private List<MethodDelegate> delegates_ = new List<MethodDelegate>();
  46.  
  47.     // ctor
  48.     public MainClass()
  49.     {
  50.         delegates_.Add(Class2.Method2);
  51.         delegates_.Add(Class3.Method3);
  52.         delegates_.Add(Class4.Method4);
  53.     }
  54.  
  55.     // Call a method
  56.     public void Method1()
  57.     {
  58.         // decide what you want to call:
  59.         delegates_[0].Invoke(); // "Class2.Method2"
  60.     } // eo Method1
  61. }  // eo class Main
  62.        
  63. using System;
  64.  
  65. namespace ConsoleApplication24
  66. {
  67.     class Program
  68.     {
  69.         static void Main(string[] args)
  70.         {
  71.             Console.WriteLine("Which method would you like to run?");
  72.             RunMyMethod(Console.ReadLine());
  73.         }
  74.  
  75.         private static void RunMyMethod(string p)
  76.         {
  77.             switch (p)
  78.             {
  79.                 case "MethodOne();":
  80.                     MethodOne();
  81.                     break;
  82.                 case "MethodTwo();":
  83.                     MethodTwo();
  84.                     break;
  85.                 case "MethodThree();":
  86.                     MethodThree();
  87.                     break;
  88.             }
  89.         }
  90.  
  91.         private static void MethodThree()
  92.         {
  93.             //Do Stuff
  94.         }
  95.  
  96.         private static void MethodTwo()
  97.         {
  98.             //Do Stuff
  99.         }
  100.  
  101.         private static void MethodOne()
  102.         {
  103.             //Do Stuff
  104.         }
  105.     }
  106. }