c# - pass callback to class -


i have class in store function call. function call can invoked class set parent class. externally supply call made, including parameters.

something like...

public class testdelegate {     public testdelegate()     {         testclass tc = new testclass(dosomething("blabla", 123, null));     }      private void dosomething(string astring, int anint, object somethingelse)     {         ...     } }  public class testclass {     public testclass(delegate method)     {         this.methodtocall = method;         this.methodtocall.execute();     }      public delegate methodtocall { get; set; } } 

when testclass class initialized call dosomething method of parent class specified parameters. should mention not want require same method signature method called. meaning not (string, int, object)

use action delegate type , create instance of closure:

public class testclass {     public testclass(action method)     {         methodtocall = method;         method();     }      public action methodtocall { get; set; } }  public class testdelegate {     public testdelegate()     {         // uses lambda syntax create closure represented in         // delegate object , passed testclass constructor.          testclass tc = new testclass(() => dosomething("blabla", 123, null));     }      private void dosomething(string astring, int anint, object somethingelse)     {         // ...     } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -