public void Test01() { dg1 += T; TempClass tc = new TempClass(); dg2 += tc.T; }
public static void Run() { Delegates delegates = new Delegates(); var thingsToDo = new List <DoThing>(); thingsToDo.Add(new DoThing(doStaticThing)); thingsToDo.Add(new DoThing(delegates.doInstanceThing)); thingsToDo.Add(delegate(string msg) { Console.WriteLine("Anonymous thing. " + msg); }); thingsToDo.Add((string msg) => { Console.WriteLine("Lambda thing. " + msg); }); Console.WriteLine("Separate delegates:"); foreach (DoThing thing in thingsToDo) { thing(); } // Delegates can be combined DoThing allThings = (string msg) => { }; foreach (DoThing thing in thingsToDo) { allThings += thing; } Console.WriteLine("Combined delegates:"); allThings(); }
private void button2_Click(object sender, EventArgs e) { Console.WriteLine("Click Start..."); DoThing method = new DoThing(DoThingLong); //同步执行委托 //method.Invoke("test"); //异步执行 //method.BeginInvoke("Test BeginInvoke",null,null); //异步执行之后执行回调 //AsyncCallback callback = t => { // Console.WriteLine("执行了回调函数"); // Console.WriteLine($"AsyncState {t.AsyncState}"); //}; //method.BeginInvoke("BeginInvoke", callback, null); //method.BeginInvoke("BeginInvoke", callback, "参数3"); Func <int, string> func = t1 => { DoThingLong("Func<int,string>"); return(t1.ToString()); }; IAsyncResult asyncResult = func.BeginInvoke(123456, t2 => { Console.Write("回调函数 {0}", Thread.CurrentThread.ManagedThreadId); Console.WriteLine($"AsyncState {t2.AsyncState}"); }, "Func<int, string>"); //等待异步执行结束 //asyncResult.AsyncWaitHandle.WaitOne(); //asyncResult.AsyncWaitHandle.WaitOne(-1); //asyncResult.AsyncWaitHandle.WaitOne(1000); //func.EndInvoke(asyncResult); string str = func.EndInvoke(asyncResult); Console.WriteLine("委托的执行结果:{0}", str); var t = Convert.ChangeType("5", typeof(int)); Console.WriteLine("Click End..."); }
public UseInjectedObjectsController(ILogger <UseInjectedObjectsController> logger, DoThing doThing, ReadConfiguration readConfiguration) { _logger = logger; _doThing = doThing; _readConfiguration = readConfiguration; }