Guest User

Untitled

a guest
Jan 16th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. public int Method1(string)
  2. {
  3. ... do something
  4. return myInt;
  5. }
  6.  
  7. public int Method2(string)
  8. {
  9. ... do something different
  10. return myInt;
  11. }
  12.  
  13. public bool RunTheMethod([Method Name passed in here] myMethodName)
  14. {
  15. ... do stuff
  16. int i = myMethodName("My String");
  17. ... do more stuff
  18. return true;
  19. }
  20.  
  21. public bool Test()
  22. {
  23. return RunTheMethod(Method1);
  24. }
  25.  
  26. public class Class1
  27. {
  28. public int Method1(string input)
  29. {
  30. //... do something
  31. return 0;
  32. }
  33.  
  34. public int Method2(string input)
  35. {
  36. //... do something different
  37. return 1;
  38. }
  39.  
  40. public bool RunTheMethod(Func<string, int> myMethodName)
  41. {
  42. //... do stuff
  43. int i = myMethodName("My String");
  44. //... do more stuff
  45. return true;
  46. }
  47.  
  48. public bool Test()
  49. {
  50. return RunTheMethod(Method1);
  51. }
  52. }
  53.  
  54. public bool RunTheMethod(Func<string, int> myMethodName)
  55. {
  56. // ... do stuff
  57. int i = myMethodName("My String");
  58. // ... do more stuff
  59. return true;
  60. }
  61.  
  62. RunTheMethod(x => x.Length);
  63.  
  64. // The <> in the name make it "unspeakable" - you can't refer to this method directly
  65. // in your own code.
  66. private static int <>_HiddenMethod_<>(string x)
  67. {
  68. return x.Length;
  69. }
  70.  
  71. public delegate int MyDelegateType(string value)
  72.  
  73. public static int Method1(string mystring)
  74. {
  75. return 1;
  76. }
  77.  
  78. public static int Method2(string mystring)
  79. {
  80. return 2;
  81. }
  82.  
  83. public bool RunTheMethod(Action myMethodName)
  84. {
  85. myMethodName();
  86. return true;
  87. }
  88.  
  89. RunTheMethod(() => Method1("MyString1"));
  90.  
  91. public static object InvokeMethod(Delegate method, params object[] args)
  92. {
  93. return method.DynamicInvoke(args);
  94. }
  95.  
  96. Console.WriteLine(InvokeMethod(new Func<string,int>(Method1), "MyString1"));
  97.  
  98. Console.WriteLine(InvokeMethod(new Func<string, int>(Method2), "MyString2"));
  99.  
  100. public static T Runner<T>(Func<T> funcToRun)
  101. {
  102. //Do stuff before running function as normal
  103. return funcToRun();
  104. }
  105.  
  106. var ReturnValue = Runner(() => GetUser(99));
  107.  
  108. public bool RunTheMethod(Func<string, int> myMethod) {
  109. // do stuff
  110. myMethod.Invoke("My String");
  111. // do stuff
  112. return true;
  113. }
  114.  
  115. public bool Test() {
  116. return RunTheMethod(Method1);
  117. }
  118.  
  119. static void MyMethod()
  120. {
  121. Console.WriteLine("I was called by the Delegate special class!");
  122. }
  123.  
  124. static void CallAnyMethod(Delegate yourMethod)
  125. {
  126. yourMethod.DynamicInvoke(new object[] { /*Array of arguments to pass*/ });
  127. }
  128.  
  129. static void Main()
  130. {
  131. CallAnyMethod(MyMethod);
  132. }
  133.  
  134. delegate void PrintDelegate(string prompt);
  135.  
  136. static void PrintSomewhere(PrintDelegate print, string prompt)
  137. {
  138. print(prompt);
  139. }
  140.  
  141. static void PrintOnConsole(string prompt)
  142. {
  143. Console.WriteLine(prompt);
  144. }
  145.  
  146. static void PrintOnScreen(string prompt)
  147. {
  148. MessageBox.Show(prompt);
  149. }
  150.  
  151. static void Main()
  152. {
  153. PrintSomewhere(PrintOnConsole, "Press a key to get a message");
  154. Console.Read();
  155. PrintSomewhere(PrintOnScreen, "Hello world");
  156. }
  157.  
  158. //parameters
  159. public void Getname(String ThisName)
  160. {
  161. txtname.Text=ThisName;
  162. }
  163.  
  164. private void button1_Click(object sender, RoutedEventArgs e)
  165. {
  166. ChildPopUp p = new ChildPopUp (Getname) //pass function name in its constructor
  167.  
  168. p.Show();
  169.  
  170. }
  171.  
  172. public Parent.FillName obj;
  173. public PopUp(Parent.FillName objTMP)//parameter as deligate type
  174. {
  175. obj = objTMP;
  176. InitializeComponent();
  177. }
  178.  
  179.  
  180.  
  181. private void OKButton_Click(object sender, RoutedEventArgs e)
  182. {
  183.  
  184.  
  185. obj(txtFirstName.Text);
  186. // Getname() function will call automatically here
  187. this.DialogResult = true;
  188. }
Add Comment
Please, Sign In to add comment