namespace Clslib//Common layer {public delegate void Writetimetodevdelegate ();//define Class}namespace Clslib//Common layer {public class Clas S1 {public clslib.writetimetodevdelegate writetime;//delegate publicly void Dosth ()//Common method { Console.WriteLine ("============================================"); Console.WriteLine ("============================================"); Console.WriteLine ("============================================"); Console.WriteLine ("============================================");
The other is the same part, but the code is not sure, the code in this method is determined by the future caller.
Because the code here is indeterminate, mutable. So we need to use a variable here. In the future, the caller will pass what code the variable is going to execute. Writetime (); Delegate variable Console.WriteLine ("============================================"); Console.WriteLine ("============================================"); Console.WriteLine ("============================================"); Console.WriteLine ("============================================"); } }}
T1 Call
Namespace T1 //t1 Project { class program { static void Main (string[] args) { Clslib.class1 c1 = new Clslib.class1 (); Real class public class C1. Writetime = M1; Give the M1 method to the entrusted C1. Dosth (); Console.readkey (); } static void M1 () { Console.WriteLine ("Current time: {0}", System.DateTime.Now.ToString ("Yyyy-mm-dd hh:mm:ss"); } }}
T2 Project Invocation
Namespace T2//t2 Project { class program { static void Main (string[] args) { Clslib.class1 c1 = new Clslib . Class1 (); C1. Writetime = M1; C1. Dosth (); Console.readkey (); } static void M1 () { file.writealltext (@ "Time.txt", System.DateTime.Now.ToString ("Yyyy-mm-dd hh:mm:ss")); Console.WriteLine ("Time has been written to file");}}}
Entrust Case 1