zoukankan
html css js c++ java
基本类型委托(二)
15
.
6 C#
对委托的支持
15
.
7
对委托链调用进行更多控制
internal
sealed
class
Light
{
public
String SwitchPosition()
{
return
"
灯关了
"
;
}
}
internal
sealed
class
Fan
{
public
String Speed()
{
throw
new
InvalidOperationException(
"
风机因过热而报废
"
);
}
}
internal
sealed
class
Speaker
{
public
String Volume()
{
return
"
声音很大
"
;
}
}
public
sealed
class
Program
{
//
查询以上各个组件状态
private
delegate
String GetStatus();
public
static
void
Main()
{
GetStatus getstatus
=
null
;
getstatus
+=
new
GetStatus(
new
Light().SwitchPosition);
getstatus
+=
new
GetStatus(
new
Fan().Speed);
getstatus
+=
new
GetStatus(
new
Speaker().Volume);
Console.WriteLine(GetStatusReport(getstatus));
Console.ReadLine();
}
private
static
String GetStatusReport(GetStatus status)
{
if
(status
==
null
)
return
null
;
StringBuilder sb
=
new
StringBuilder();
Delegate[] arrayOfDelegate
=
status.GetInvocationList();
foreach
(GetStatus s
in
arrayOfDelegate)
{
try
{
sb.AppendFormat(
"
{0}{1}{1}
"
, s(), Environment.NewLine);
}
catch
(InvalidOperationException e)
{
Object component
=
s.Target;
sb.AppendFormat(
"
Failed to get status from{1}{2}{0}Error:{3}{0}{0}
"
,
Environment.NewLine,
((component
==
null
)
?
""
: component.GetType()
+
"
.
"
),
s.Method.Name,
e.Message);
}
}
return
sb.ToString();
}
}
15
.
8C#
为委托提供的语法便利
作者:
青羽
查看全文
相关阅读:
17张程序员壁纸推荐,是否有一张你喜欢的?
学会了这些英文单词,妈妈再也不用担心我学不会Python
小白学习Python英语基础差怎么办,都帮你想好拉!看这里
自动化测试学习防踩坑手册,测试人员人手一份
Selenium自动化结合Mysql数据项目实战操作
解除你学习Python自动化测试框架的所有疑惑,开启学习直通车
数据库管理软件navicate12的激活和安装
修改文件版本号方法
Json的数据映射(基于LitJson)
VMware 虚拟机安装黑屏问题
原文地址:https://www.cnblogs.com/tenghoo/p/1208443.html
最新文章
读的
boost用法
【C++14 | C++17】std::tuple的用法
【activemq】相关札记
微信和支付宝异步回调通知IP白名单
数据库建模三步骤:概念模型->逻辑模型->物理模型
SpringCloud之服务注册与发现Eureka
SpringBoot使用轻量级HTTP请求工具Forest
Spring中的重试机制Retry的注意事项
kanzi工程加载不出来子模块的问题解决
热门文章
ps取消选中快捷键
ps弹出卡尺快捷键
kanzi Tigger使用遇到的问题总结
js下载文件 兼容性写法
算法-11| 最短路径| Dijkstra算法
影响网络信息传播的因素
0297. Serialize and Deserialize Binary Tree (H)
0124. Binary Tree Maximum Path Sum (H)
Fiddler抓HTTPS接口数据,安装证书并不复杂,超详细的图文解说,不信你看!
程序员!一款超实用的安卓实时同步投屏电脑的软件,谁用谁知道!
Copyright © 2011-2022 走看看