委托是一种定义方法签名的类型。当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联。您可以通过委托实例调用方法。

委托就是执行方法(函数)的一个类。

事件是一种特殊的委托。

        public delegate int TestDelegate(int x, int y);

       Action是无返回值的泛型委托。

Action 表示无参,无返回值的委托

Action<int,string> 表示有传入参数int,string无返回值的委托

Func是有返回值的泛型委托

Func<int> 表示无参,返回值为int的委托

Func<object,string,int> 表示传入参数为object, string 返回值为int的委托

predicate 是返回bool型的泛型委托

predicate<int> 表示传入参数为int 返回bool的委托。

Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型

Action至少1个参数,至多4个参数,无返回值,

Func至少0个参数,至多4个参数,根据返回值泛型返回。必须有返回值,不可void

Predicate至少1个参数,至多1个参数,返回值固定为bool

TestDelegate d2= (string name) => { Console.WriteLine(“你好,{0}!”, name); };

d2(“Terry”);

delegate void TestDelegate(string myName);

TestDelegate d2 = delegate(string name)
{

Console.WriteLine(“Hello,{0}!”, name);

};

d2(“Test”);

private void DelegateMethod(string name)

{

Console.WriteLine(“Hello,{0}!”, name);

       }

       TestDelegate d2 = new TestDelegate(DelegateMethod);

       d2(“Test”);

委托类似于 C++ 函数指针,但它们是类型安全的。

委托允许将方法作为参数进行传递。

委托可用于定义回调方法。

委托可以链接在一起;例如,可以对一个事件调用多个方法。

方法不必与委托签名完全匹配。

委托一般都使用在 Observer模式(观察者模式)。

Observer设计模式是为了定义对象间的一种一对多的依赖关系,以便于当一个对象的状态改变时,其他依赖于它的对象会被自动告知并更新。

Observer模式主要包括如下两类对象:

被监视对象:往往包含着其他对象所感兴趣的内容。

监视者:当对象中的某件事发生的时候,会告知建设者,而建设者则会采取相应的行动。

例如:当你程序处理大批量数据时,需要在程序界面显示进度条进行友好提示,这时你通过委托来实现相当方便。

范例:

public delegate void DelegateMethod(int position, int maxValue);

  1.   public class TestDelegate
        {
            public DelegateMethod OnDelegate;

        public void DoDelegateMethod()
        {
            int maxValue = 100;
            for (int i = 0; i < maxValue; i++)
            {
                if (this.OnDelegate != null)
                {
                    this.OnDelegate(i, maxValue);
                }
            }
        }

  1. TestDelegate test = new TestDelegate();
                this.textBox1.Text = "";
                this.progressBar1.Value = 0;
                test.OnDelegate = new DelegateMethod(delegate(int i, int maxValue)
                {
                    this.textBox1.Text += i.ToString() + Environment.NewLine;
                    this.progressBar1.Maximum = maxValue;
                    this.progressBar1.Value++;
                });
                test.DoDelegateMethod();

1、在类中申明清空委托方法,依次循环去除委托引用。

         方法如下:

public class TestDelegate
    {
        public DelegateMethod OnDelegate;

  1.                  public void ClearDelegate()
            {
                while (this.OnDelegate != null)
                {
                    this.OnDelegate -= this.OnDelegate;
                }
            }
        }
  1. 2、如果在类中没有申明清空委托的方法,我们可以利用GetInvocationList查询出委托引用,然后进行去除。

方法如下:

TestDelegate test = new TestDelegate();

if (test.OnDelegate != null)
{
  System.Delegate[] dels = test.OnDelegate.GetInvocationList();
  for (int i = 0; i < dels.Length; i++)
  {
     test.OnDelegate -= dels[i] as DelegateMethod;
  }
}

功能需求:查询打印机的墨粉量,如果低于50时则发送Email邮件到客户进行提醒。

优化前代码

namespace DelegateExample.Before
{
    public class SpyPrinterToner
    {
        public void CheckPrinterTonerIsLower()
        {
            PhysicalPrinterAction action = new PhysicalPrinterAction();
            int remainToner = action.SelectPrinterToner();
            if (remainToner < 50)
            {
                MessageController controller = new MessageController();
                controller.SendMessage(“Printer Name”);
            }
        }
    }
 
    public class MessageController
    {
        public void SendMessage(string printerName)
        {
            //TODO: SendMessage
        }
    }
 
    public class PhysicalPrinterAction
    {
        public int SelectPrinterToner()
        {
            return 80;
        }
    }
}

调用:

            DelegateExample.Before.SpyPrinterToner toner = new Before.SpyPrinterToner();
            toner.CheckPrinterTonerIsLower();

以上代码也可以说采用了面向对象编程,但是SpyPrinterToner 与 MessageController 之间存在了不必要的耦合度, 造成了日后的程序维护的工作量以及不便于程序的扩展。

那我们该如何降低 SpyPrinterToner 与 MessageController 之间的耦合度,从而达到:高内聚,低耦合的目的。

显而易见我们利用观察者模式可以达到。

优化后的代码

namespace DelegateExample.After
{
 
 
    public class SpyPrinterToner
    {
        public Action<string> OnSendMessage;
 
        public void CheckPrinterTonerIsLower()
        {
            PhysicalPrinterAction action = new PhysicalPrinterAction();
            int remainToner = action.SelectPrinterToner();
            if (remainToner < 50)
            {
                if (this.OnSendMessage != null)
                {
                    this.OnSendMessage(“Printer Name”);
                }
            }
        }
    }
 
    public class MessageController
    {
        public void SendMessage(string printerName)
        {
            //TODO: SendMessage
        }
    }
 
    public class PhysicalPrinterAction
    {
        public int SelectPrinterToner()
        {
            return 80;
        }
    }
}

调用

DelegateExample.After.SpyPrinterToner toner = new After.SpyPrinterToner();
toner.OnSendMessage += new Action<string>(new After.MessageController().SendMessage);
toner.CheckPrinterTonerIsLower();

进行这样的优化之后,2个类直接的耦合度降低了。

如果日后需求进行了更改,需要增加IM类型的消息或者其他类型的消息类别,那我们则只需要再增加一个委托即可,如果不采用委托去实现,则SpyPrinterToner类又会与IM处理类或者其他类相互耦合。

在项目开发过程中经常会看到类似的代码:

try
            {
                Do();
            }
            catch (Exception ex)
            {
                LogException(ex);
            }
            finally
            {
                DoFinally();
            }

造成代码量的冗余,给日后代码维护带来很多的不便。

有很多种方法可以实现,例如:AOP、委托等。在这里我们主要讲如何利用Func委托来实现代码优化。

 
        private void CallMethod(Func<string> func)
        {
            try
            {
                func();
            }
            catch (Exception ex)
            {
                LogException(ex);
            }
            finally
            {
                DoFinally();
            }
        }

CallMethod(new Func<string>(Do));

我们将方法作为委托进行传入,这样节省了很多的冗余代码。

 

 

 

 

 

 

 

 

 

 

委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用if-else(switch)语句,同时使得程序具有更好的可扩展性。

使用委托可以将多个方法绑定到同一个委托变量上(通常称这个委托变量为:委托链),当调用此变量时,会依次调用所有绑定的方法;于此同时,也可以通过类似绑定的方式删除方法。

 

一个简单的委托例子

复制代码
  1. using System;
  2. using System.Collections;
  3.  
  4. namespace Delegate
  5. {
  6. //用delegate关键字声明一个委托
  7. //委托原型必须与预委托的方法具有相同的返回值和参数类型
  8. delegate void LearnDelegate(string name);
  9.  
  10. public class Test
  11. {
         //要进行委托的方法
  12. static void LearnA(string name)
  13. {
  14. Console.WriteLine("小王在学" + name);
  15. }
  16.       
  17. static void LearnB(string name)
  18. {
  19. Console.WriteLine("小张在学" + name);
  20. }
  21.  
  22. static void Main()
  23. {
  24. //创建委托实例,并把委托函数名当作参数传递给委托对象
  25. LearnDelegate learn_A = new LearnDelegate(LearnA);
  26. LearnDelegate learn_B = new LearnDelegate(LearnB);
  27.  
  28. //通过委托对象,给绑定的函数传递参数,其实现在的learn_A和learn_B就是learnA和learnB的函数别名
  29. learn_A("C#");
  30. learn_B("C++");
  31. }
  32. }
  33. }
复制代码

 

委托链

复制代码
  1. static void Main()
  2. {
  3.   //创建委托实例
  4.   LearnDelegate learn_A = new LearnDelegate(LearnA);
  5.   LearnDelegate learn_B = new LearnDelegate(LearnB);
  6.  
  7.   //声明一个委托链,不需要实例化它
  8.   LearnDelegate DelegateLst;
  9.   //把委托对象直接相加并赋给委托链
  10.   DelegateLst = learn_A + learn_B;
  11.   //给委托链传值
  12.   DelegateLst("编程");
  13.  
  14.   Console.WriteLine();
  15.  
  16.   //同样,也可以对委托链中的委托对象进行添加和删除操作
  17.  
  18.   DelegateLst -= learnA;//删除一个委托方法,这里使用DelegateLst -= Learn_A;效果是一样的,因为此时可以看作它是LearnA方法的一个别名
  19.   DelegateLst("编程");
  20.   Console.WriteLine();
  21.   DelegateLst += learnB;//添加一个委托方法,同样这里也可以使用DelegateLst += Learn_B;
  22.   DelegateLst("编程");
  23. }
复制代码

 

委托链的简化使用

复制代码
  1. using System;
  2. using System.Collections;
  3.  
  4. namespace Delegate
  5. {
  6. //用delegate关键字声明一个委托
  7. //委托原型必须与预委托的方法具有相同的返回值和参数类型
  8. delegate void LearnDelegate(string name);
  9.  
  10. public class Test
  11. {
  12. static void Main()
  13. {
  14. //声明一个委托链,赋null值
  15. LearnDelegate DelegateLst = null;
  16. //下面这种形式其实是匿名方法的一种应用
  17. DelegateLst += delegate(string name) { Console.WriteLine("小王在学" + name); };
  18. DelegateLst += delegate(string name) { Console.WriteLine("小张在学" + name); };
  19. DelegateLst("编程");
  20. }
  21. }
  22. }
复制代码

 

最后的一个例子

复制代码
  1. using System;
  2. using System.Collections;
  3.  
  4. namespace Delegate
  5. {
  6. //用delegate关键字声明一个委托
  7. //委托原型必须与预委托的方法具有相同的返回值和参数类型
  8. delegate void AnimalDelegate(string AnimalName);
  9. class Animal
  10. {
  11. public Animal()
  12. {
  13.  
  14. }
  15.  
  16. //要进行委托的方法
  17. public void Miaow(string name)
  18. {
  19. Console.WriteLine( name + " 喵喵叫");
  20. }
  21. //要进行委托的方法
  22. public void Bark(string name)
  23. {
  24. Console.WriteLine(name + " 汪汪叫");
  25. }
  26. //要进行委托的方法
  27. public void Baa(string name)
  28. {
  29. Console.WriteLine(name + " 咩...");
  30. }
  31. }
  32.  
  33. public class MainFunc
  34. {
  35. static void choseAnimal(string name, AnimalDelegate delegateFun)
  36. {
  37. delegateFun(name);
  38. }
  39. static void Main()
  40. {
  41. /*AnimalDelegate cat = new AnimalDelegate(new Animal().Miaow);
  42. cat("猫咪");
  43. AnimalDelegate dog = new AnimalDelegate(new Animal().Bark);
  44. dog("狗狗");*/
  45. //上下的两种调用方式是等价的
  46.  
  47. choseAnimal("喵星人", new Animal().Miaow);
  48. choseAnimal("汪星人", new Animal().Bark);
  49. choseAnimal("喜洋洋", new Animal().Baa);
  50. }
  51. }
  52. }
复制代码

版权声明:本文为eric-qin原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/eric-qin/p/4155882.html