C# 委托应用总结

一、什么是委托

1.1官方解释

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

1.2个人理解

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

事件是一种特殊的委托。

二、如何申明委托

2.1 delegate

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

2.2 Action

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

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

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

2.3 Func

Func是有返回值的泛型委托

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

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

2.4 predicate

predicate 是返回bool型的泛型委托

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

2.5 四者之间的区别

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

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

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

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

三、如何使用委托

3.1 Labmda表达式

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

d2("Terry");

3.2匿名方法

delegate void TestDelegate(string myName);

TestDelegate d2 = delegate(string name)
{

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

};

d2(“Test”);

3.3 函数申明

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);

  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);
                }
            }
        }

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;

                 public void ClearDelegate()
        {
            while (this.OnDelegate != null)
            {
                this.OnDelegate -= this.OnDelegate;
            }
        }
    }
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处理类或者其他类相互耦合。

八、利用Func委托代码优化

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

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));

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

http://www.cnblogs.com/foolishfox/archive/2010/09/16/1827964.html

C# 委托应用总结(委托,Delegate,Action,Func,predicate)的更多相关文章

  1. Delegate,Action,Func,Predicate的使用与区别

    C#4.0推出后,类似Linq,Lamda表达式等许多新的程序写法层次不穷.与之相关的Delegate,Action,Func,Predicate的使用和区别也常常让大家迷惑,此处就结合实际的应用,对 ...

  2. 委托delegate,Action,Func,Predicate

    C#委托的介绍(delegate.Action.Func.predicate) 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 ...

  3. (C#) Action, Func, Predicate 等泛型委托

    (转载网络文章) (1). delegate delegate我们常用到的一种声明   Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型.   例:public del ...

  4. Delegate,Action,Func,匿名方法,匿名委托,事件 (转载)

    Delegate,Action,Func,匿名方法,匿名委托,事件 (转载) 一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本 ...

  5. Delegate,Action,Func,匿名方法,匿名委托,事件

    一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本的数据类型(或者没有参数),比如 public void HelloWorld ...

  6. c# Action,Func,Predicate委托

    System命名空间下已经预先定义好了三中泛型委托,Action,Func和Predicate,这样我们在编程的时候,就不必要自己去定义这些委托了 Action是没有返回值的 Func是带返回值的 不 ...

  7. 【Unity|C#】基础篇(11)——内置的泛型委托(Action/Func/Predicate)

    [Action] 无返回值 的泛型委托,可以有0~16个参数(函数重载) public delegate void Action(); // 无参数 public delegate void Acti ...

  8. 温故而知新:Delegate,Action,Func,匿名方法,匿名委托,事件

    Tks: http://www.cnblogs.com/yjmyzz/archive/2009/11/23/1608818.html 20150801 add: http://www.cnblogs. ...

  9. 委托、多播委托、泛型委托Func,Action,Predicate,ExpressionTree

    当试图通过一个事件触发多个方法,抽象出泛型行为的时候,或许可以考虑使用委托.     通过委托构造函数或委托变量把方法赋值给委托 private delegate double DiscountDel ...

随机推荐

  1. pytest(10)-常用执行参数说明

    pytest单元测试框架中可以使用命令行及代码pytest.main()两种方式执行测试,且可以加入各种参数来组织执行测试.接下来我们来了解常用的执行参数的含义及其用法. pytest中的执行参数根据 ...

  2. 【论文考古】联邦学习开山之作 Communication-Efficient Learning of Deep Networks from Decentralized Data

    B. McMahan, E. Moore, D. Ramage, S. Hampson, and B. A. y Arcas, "Communication-Efficient Learni ...

  3. elk监听Java日志发送微信报警

    一年前写过logstash根据日志关键词报警 ,今年重温一下.并且记录一下遇到的问题解决办法. Java错误日志一般出现一大坨,如下图: 所以我们的filebeat日志收集器就要改成多行匹配模式,以日 ...

  4. MongoDB 带访问控制的副本集部署

    当你需要用到一个MongoDB 副本集集群,用于开发测试时, 可以通过下面的步骤简单完成. 版本及环境 MongoDB4.4  Centos6.5  一. 下载安装 MongoDB Server 及 ...

  5. gulp更新4.0后的报错(gulp报Did you forget to signal async completion?)

    本文首发于青云工作室 原文链接为 https://qystudio.ltd/posts/55153.html 缘起 今天我升级了gulp到4.0,在git三件套之后,网站并没有更新,我便登录了gith ...

  6. IDEA tomcat启动报错----Artifact is being deployed, please wait...解决

    今天学习遇到了这个错误,记录下自己遇到的错误和解决方法! 这个报错的意思是: Artifact 正在部署中,请稍候- 实际上有可能就是jar包没有导进去.检查项目打包情况:file-->Proj ...

  7. 编写资源yaml文件、压力机配置hosts

    资源文件 Deployment/StatefulSet/DaemonSet.Service.Ingress等 参考:https://www.cnblogs.com/uncleyong/p/155710 ...

  8. Window常用快捷键

    Window常用快捷键 Ctrl+C:复制 Ctrl+V:粘贴 Ctrl+A:全选 Ctrl+X:剪切 Ctrl+D:删除 Ctrl+Z:撤销 Ctrl+Y:反撤销 Ctrl+Shift+Esc:调出 ...

  9. CultureInfo、DateTimeFormatInfo、NumberFormatinfo之间的关系

    CultureInfo.DateTimeFormatInfo.NumberFormatinfo之间的关系 线程中CurrentCulture和CurrentUICulture 区别 以下是win10操 ...

  10. 小程序swiper高度自适应解决方案

    scroll-view 里面继续套一个 scroll-view ,设置纵向允许滚动 <swiper class="swiper"> <swiper-item> ...