C# delegate multicast single delegate
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApp386
{
class Program
{
static void Main(string[] args)
{
Thermostat thermostat = new Thermostat();
Heater heater = new Heater();
Cooler cooler = new Cooler(); Action<float> del1;
Action<float> del2;
Action<float> del3;
del1 = heater.OnTemperatureChanged;
del2 = cooler.OnTemperatureChanged;
Console.WriteLine("Invoke both delegates:");
del3 = del1+del2; del3();
Console.WriteLine("Multicast delegates:");
foreach(var dl in del3.GetInvocationList())
{
Console.WriteLine($"Target:{dl.Target},Method:{dl.Method}");
} Console.WriteLine();
Console.WriteLine("Invoke only del2:");
del3 = del3 - del1;
del3(); Console.WriteLine("\nSingle delegate:");
foreach (var dl in del3.GetInvocationList())
{
Console.WriteLine($"Target:{dl.Target},Method:{dl.Method}");
} Console.ReadLine();
}
} class Cooler
{
public float Temperature
{
get;set;
}
public Cooler(float temperature)
{
Temperature = temperature;
} public void OnTemperatureChanged(float newTemperature)
{
if(newTemperature>Temperature)
{
Console.WriteLine("Cooloer:On");
}
else
{
Console.WriteLine("Cooler:Off");
}
}
} class Heater
{
public float Temperature
{
get;set;
}
public Heater(float temperature)
{
Temperature = temperature;
} public void OnTemperatureChanged(float newTemperature)
{
if(newTemperature<Temperature)
{
Console.WriteLine("Heater:On");
}
else
{
Console.WriteLine("Heater:Off");
}
}
} public class Thermostat
{
//Define the event publisher
public Action<float> OnTemperatureChange { get; set; } private float currentTemperatureValue;
public float CurrentTemperature
{
get
{
return currentTemperatureValue;
}
set
{
if(value!=currentTemperatureValue)
{
currentTemperatureValue = value;
OnTemperatureChange?.Invoke(value);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApp386
{
class Program
{
static void Main(string[] args)
{
Thermostat thermostat = new Thermostat();
Heater heater = new Heater();
Cooler cooler = new Cooler(); Action<float> del1;
Action<float> del2;
Action<float> del3;
del1 = heater.OnTemperatureChanged;
del2 = cooler.OnTemperatureChanged;
Console.WriteLine("Invoke both delegates:");
del3 = del1;
del3 += del2;
del3();
Console.WriteLine("Multicast delegates:");
foreach(var dl in del3.GetInvocationList())
{
Console.WriteLine($"Target:{dl.Target},Method:{dl.Method}");
}
Console.WriteLine();
Console.WriteLine("Invoke only del2:");
del3 -= del1;
del3(); Console.WriteLine("\nSingle delegate:");
foreach (var dl in del3.GetInvocationList())
{
Console.WriteLine($"Target:{dl.Target},Method:{dl.Method}");
}
Console.ReadLine();
}
} class Cooler
{
public float Temperature
{
get;set;
}
public Cooler(float temperature)
{
Temperature = temperature;
} public void OnTemperatureChanged(float newTemperature)
{
if(newTemperature>Temperature)
{
Console.WriteLine("Cooloer:On");
}
else
{
Console.WriteLine("Cooler:Off");
}
}
} class Heater
{
public float Temperature
{
get;set;
}
public Heater(float temperature)
{
Temperature = temperature;
} public void OnTemperatureChanged(float newTemperature)
{
if(newTemperature<Temperature)
{
Console.WriteLine("Heater:On");
}
else
{
Console.WriteLine("Heater:Off");
}
}
} public class Thermostat
{
//Define the event publisher
public Action<float> OnTemperatureChange { get; set; } private float currentTemperatureValue;
public float CurrentTemperature
{
get
{
return currentTemperatureValue;
}
set
{
if(value!=currentTemperatureValue)
{
currentTemperatureValue = value;
OnTemperatureChange?.Invoke(value);
}
}
}
}
}
C# delegate multicast single delegate的更多相关文章
- 在OC中调用Swift类中定义delegate出现:Property 'delegate' not found on object of type ...
找了许久没找到答案, 在下面的链接中, 我解决了这个问题: http://stackoverflow.com/questions/26366082/cannot-access-property-of- ...
- (转)C# Delegate.Invoke、Delegate.BeginInvoke
Delegate的Invoke.BeginInvoke 1.Delegate.Invoke (委托同步调用) a.委托的Invoke方法,在当前线程中执行委托. b.委托执行时阻塞当前线程,知道委托执 ...
- C#Delegate.Invoke、Delegate.BeginInvoke And Control.Invoke、Control.BeginInvoke
作者:EasonLeung 一.Delegate的Invoke.BeginInvoke 1.Delegate.Invoke (委托同步调用) a.委托的Invoke方法,在当前线程中执行委托. b.委 ...
- c# 关键字delegate、event(委托与事件)[MSDN原文摘录][1]
A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. ...
- JSBinding + SharpKit / 原理篇:Delegate
以 NGUI 的 UIEventListener 为例: 有一个类: using SharpKit.JavaScript; using UnityEngine; using System.Collec ...
- 【转】iOS 开发之协议protocal-代理传值delegate
原文网址:http://www.cnblogs.com/wzrong/p/3201938.html 刚开始做iOS开发的时候,对 protocol.delegate 的理解一直都是晕晕乎乎一知半解的状 ...
- C#中的委托(Delegate)和事件(Event)
原文地址:C#中的委托(Delegate)和事件(Event) 作者:jiyuan51 把C#中的委托(Delegate)和事件(Event)放到现在讲是有目的的:给下次写的设计模式--观察者(Obs ...
- C# Note2:委托(delegate) & Lambda表达式 & 事件(event)
前言 本文主要讲述委托和Lambda表达式的基础知识,以及如何通过Lambda表达式实现委托调用,并阐述.NET如何将委托用作实现事件的方式. 参考:C#高级编程 1.什么是委托(delegate)? ...
- Delegate,Action,Func,匿名方法,匿名委托,事件
一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本的数据类型(或者没有参数),比如 public void HelloWorld ...
随机推荐
- vue定义全局date过滤器(自定义JS文件模块和Moment.js库)
自定义dateFormat.js文件模块 dateFormat.js /** * 时间字符串 转 时间戳 * @param {String} time_str 时间字符串(格式"2014-0 ...
- 围观高手是如何写好 Python 循环,把内存用到极致的?
0 前言 说到处理循环,我们习惯使用for, while等,比如依次打印每个列表中的字符: lis = ['I', 'love', 'python'] for i in lis: print( ...
- opencv---(腐蚀、膨胀、边缘检测、轮廓检索、凸包、多边形拟合)
一.腐蚀(Erode) 取符合模板的点, 用区域最小值代替中心位置值(锚点) 作用: 平滑对象边缘.弱化对象之间的连接. opencv 中相关函数:(erode) // C++ /** shape: ...
- 好用的js片段收藏
1.判断浏览器信息,如果是手机,就跳到手机页面 if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) { wind ...
- SAP 表汇总
SAP 表整理:VBKPF-预制凭证抬头表: VBKPF-预制凭证抬头表 VBKPF-预制凭证抬头表 VBSEG-预制凭证行项目表: VBSEG-预制凭证行项目表 VBSEG-预制凭证行项目表 VBS ...
- Mybatis专题
Java后端知识点汇总——Java基础专题 全套Java知识点汇总目录,见https://www.cnblogs.com/autism-dong/p/11831922.html 1.什么是Mybati ...
- Prism_Commanding(2)
Commanding 除了提供对要在视图中显示或编辑的数据的访问之外,ViewModel还可能定义可由用户执行的一个或多个动作或操作.用户可以通过UI执行的动作或操作通常被定义为命令.命令提供了一种方 ...
- 利用Azure虚拟机安装Dynamics 365 Customer Engagement之七:安装前端服务器及部署管理器
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...
- 常用adb命令总结
前言 很早就想整理一下自己平时常用的一些adb命令,不仅为了便于以后查找,而且整理的过程自己又重新复习了一遍,但是当我开始在度娘一搜的时候,发现很多人已经写的非常详细了,尤其是当我发现了这篇adb概括 ...
- css字体标签相关
斜体: i:斜体em:斜体,强调的意思,有特殊含义,尽量用i或者cssfont-style:字体风格 normal默认值,italic斜体,oblique倾斜的字体 粗体: b,strong:粗体fo ...