- 代理, 将方法作为另一方法的参数. 类似C里面的函数指针.
using System; using System.Windows.Forms; using System.Threading; namespace Demo { public delegate void Greeting(string name); public class Demo { private static void GreetingEnglish(string name) { Console.WriteLine ("Hello,{0}", name); } private static void GreetingChinese(string name) { Console.WriteLine ("你好,{0}", name); } //委托,将方法作为另一方法的参数.委托可以代表一系列返回值和参数类型与其一致的方法. private static void GreetingPeople(string name, Greeting greet) { greet (name); } public static void Main() { GreetingPeople ("Jeffrey Richter", GreetingEnglish); GreetingPeople ("刘德华", GreetingChinese); Console.ReadKey (); } } }
- 将方法绑定到委托
using System; using System.Threading; namespace Demo { public delegate void Greeting(string name); public class Demo { private static void GreetingEnglish(string name) { Console.WriteLine ("Hello,{0}", name); } private static void GreetingChinese(string name) { Console.WriteLine ("你好,{0}", name); } private static void GreetingFrance(string nom) { Console.WriteLine ("Bonjour,{0}", nom); } //delegate: use the Method name as Parameters. private static void GreetingPeople(string name, Greeting greet) { greet (name); } public static void Main() { GreetingPeople ("Jeffrey Richter", GreetingEnglish); GreetingPeople ("刘德华", GreetingChinese); //bind Method to Delegate Greeting chinese = GreetingChinese; Greeting english = GreetingEnglish; english += GreetingFrance; chinese ("许明会"); english ("Matt Petrek"); Console.ReadKey (); } } }
- 委托与接口
using System; using System.Threading; namespace Demo { interface IGreeting{ void Greeting(string name); }; public class GreetingChinese:IGreeting { public void Greeting(string name) { Console.WriteLine ("你好,{0}", name); } } public class GreetingEnglish:IGreeting { public void Greeting(string name) { Console.WriteLine ("Hello,{0}", name); } } public class Demo { private static void Greeting(string name, IGreeting greeting) { greeting.Greeting (name); } public static void Main() { IGreeting chinese = new GreetingChinese (); IGreeting english = new GreetingEnglish (); chinese.Greeting ("许明会"); english.Greeting ("Jeffrey Richter"); Greeting ("Matt Petrik", english); Console.ReadKey (); } } }
- 更好的封装,事件的由来
using System; using System.Threading; namespace Demo { public delegate void Greet(string name); public class GreetingManager { public Greet deleGreet; public void Greeting(string name, Greet greet) { greet (name); } } public class Demo { private static void GreetingEnglish(string name) { Console.WriteLine ("Hello,{0}", name); } private static void GreetingChinese(string name) { Console.WriteLine ("你好,{0}", name); } //Main() public static void Main() { GreetingManager gm = new GreetingManager (); gm.deleGreet = GreetingChinese; gm.deleGreet += GreetingEnglish ; gm.Greeting ("Matt Petrik",gm.deleGreet); Console.ReadKey (); } } }
- 更好的封装,准备引入事件
using System; using System.Threading; namespace Demo { public delegate void Greet(string name); public class GreetingManager { public Greet deleGreet; public void Greeting(string name) { if (deleGreet!=null) { deleGreet(name); } } } public class Demo { private static void GreetingEnglish(string name) { Console.WriteLine ("Hello,{0}", name); } private static void GreetingChinese(string name) { Console.WriteLine ("你好,{0}", name); } //Main() public static void Main() { GreetingManager gm = new GreetingManager (); gm.deleGreet = GreetingChinese; gm.deleGreet += GreetingEnglish ; gm.Greeting ("Matt Petrik"); Console.ReadKey (); } } }
- 委托的目的是把它暴露到外面,以让客户端进行方法注册。Event保证在类的内部总是Private,可认为是为Delegate定制的Property。
using System; using System.Threading; namespace Demo { public delegate void Greet(string name); public class GreetingManager { //public Greet deleGreet; public event Greet EventGreet; public void Greeting(string name) { if (EventGreet!=null) EventGreet(name); } } public class Demo { private static void GreetingEnglish(string name) { Console.WriteLine ("Hello,{0}", name); } private static void GreetingChinese(string name) { Console.WriteLine ("你好,{0}", name); } //Main() public static void Main() { GreetingManager gm = new GreetingManager (); gm.EventGreet += GreetingChinese; gm.EventGreet += GreetingEnglish ; //Greeting call from client, that's not good! gm.Greeting ("Matt Petrik"); Console.ReadKey (); } } }
- .NET框架的委托和事件
的