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#
为委托提供的语法便利
作者:
青羽
查看全文
相关阅读:
continue用法
break用法
VLAN的划分
子网掩码的计算
简述RIP路由协议和OSPF路由协议的相同点和不同点。
工程监理的内容是什么?
工程监理的意义和职责是什么?
双绞线测试的参数主要有哪些?
光纤熔接损耗原因?
综合布线系统的设计等级有哪几种?各有什么特点?
原文地址:https://www.cnblogs.com/tenghoo/p/1208443.html
最新文章
Commons FileUpload文件上传组件
Spring 开发第一步(四)Spring与JDBC事务
Spring 开发第一步(三)Spring与JDBC
Eclipse (JavaEE版)中修改web项目的访问路径
Spring 开发第一步(二)
Spring 开发第一步
使用libxml2进行xml开发(一)
IBM主机家族——大型机、中型机、小型机
Unix传奇
Unix awk使用手册
热门文章
css定位流布局
css过渡模块和2d转换模块
进入html+css世界的正确姿势
css盒子模型
css浮动布局
C#中反射的概念及其使用(转)
c#中 ==与equals有什么区别
Select与SelectMany的区别
jquery.cookie中的操作
深入了解setInterval方法
Copyright © 2011-2022 走看看