Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int Method1(string)
- {
- ... do something
- return myInt;
- }
- public int Method2(string)
- {
- ... do something different
- return myInt;
- }
- public bool RunTheMethod([Method Name passed in here] myMethodName)
- {
- ... do stuff
- int i = myMethodName("My String");
- ... do more stuff
- return true;
- }
- public bool Test()
- {
- return RunTheMethod(Method1);
- }
- public class Class1
- {
- public int Method1(string input)
- {
- //... do something
- return 0;
- }
- public int Method2(string input)
- {
- //... do something different
- return 1;
- }
- public bool RunTheMethod(Func<string, int> myMethodName)
- {
- //... do stuff
- int i = myMethodName("My String");
- //... do more stuff
- return true;
- }
- public bool Test()
- {
- return RunTheMethod(Method1);
- }
- }
- public bool RunTheMethod(Func<string, int> myMethodName)
- {
- // ... do stuff
- int i = myMethodName("My String");
- // ... do more stuff
- return true;
- }
- RunTheMethod(x => x.Length);
- // The <> in the name make it "unspeakable" - you can't refer to this method directly
- // in your own code.
- private static int <>_HiddenMethod_<>(string x)
- {
- return x.Length;
- }
- public delegate int MyDelegateType(string value)
- public static int Method1(string mystring)
- {
- return 1;
- }
- public static int Method2(string mystring)
- {
- return 2;
- }
- public bool RunTheMethod(Action myMethodName)
- {
- myMethodName();
- return true;
- }
- RunTheMethod(() => Method1("MyString1"));
- public static object InvokeMethod(Delegate method, params object[] args)
- {
- return method.DynamicInvoke(args);
- }
- Console.WriteLine(InvokeMethod(new Func<string,int>(Method1), "MyString1"));
- Console.WriteLine(InvokeMethod(new Func<string, int>(Method2), "MyString2"));
- public static T Runner<T>(Func<T> funcToRun)
- {
- //Do stuff before running function as normal
- return funcToRun();
- }
- var ReturnValue = Runner(() => GetUser(99));
- public bool RunTheMethod(Func<string, int> myMethod) {
- // do stuff
- myMethod.Invoke("My String");
- // do stuff
- return true;
- }
- public bool Test() {
- return RunTheMethod(Method1);
- }
- static void MyMethod()
- {
- Console.WriteLine("I was called by the Delegate special class!");
- }
- static void CallAnyMethod(Delegate yourMethod)
- {
- yourMethod.DynamicInvoke(new object[] { /*Array of arguments to pass*/ });
- }
- static void Main()
- {
- CallAnyMethod(MyMethod);
- }
- delegate void PrintDelegate(string prompt);
- static void PrintSomewhere(PrintDelegate print, string prompt)
- {
- print(prompt);
- }
- static void PrintOnConsole(string prompt)
- {
- Console.WriteLine(prompt);
- }
- static void PrintOnScreen(string prompt)
- {
- MessageBox.Show(prompt);
- }
- static void Main()
- {
- PrintSomewhere(PrintOnConsole, "Press a key to get a message");
- Console.Read();
- PrintSomewhere(PrintOnScreen, "Hello world");
- }
- //parameters
- public void Getname(String ThisName)
- {
- txtname.Text=ThisName;
- }
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- ChildPopUp p = new ChildPopUp (Getname) //pass function name in its constructor
- p.Show();
- }
- public Parent.FillName obj;
- public PopUp(Parent.FillName objTMP)//parameter as deligate type
- {
- obj = objTMP;
- InitializeComponent();
- }
- private void OKButton_Click(object sender, RoutedEventArgs e)
- {
- obj(txtFirstName.Text);
- // Getname() function will call automatically here
- this.DialogResult = true;
- }
Add Comment
Please, Sign In to add comment