22.3 Delegate invocation

原创 2006年06月22日 03:19:00
C# provides special syntax for invoking a delegate. When a non-null delegate
instance whose invocation list
contains one entry, is invoked, it invokes the one method with the same
arguments it was given, and returns the
same value as the referred to method. (See §14.5.5.2 for detailed
information on delegate invocation.) If an
exception occurs during the invocation of such a delegate, and that
exception is not caught within the method that
was invoked, the search for an exception catch clause continues in the
method that called the delegate, as if that
method had directly called the method to which that delegate referred.
Invocation of a delegate instance whose invocation list contains multiple
entries, proceeds by invoking each of
the methods in the invocation list, synchronously, in order. Each method so
called is passed the same set of
arguments as was given to the delegate instance. If such a delegate
invocation includes reference parameters
(§17.5.1.2), each method invocation will occur with a reference to the
same variable; changes to that variable by
one method in the invocation list will be visible to methods further down
the invocation list. If the delegate
invocation includes output parameters or a return value, their final value
will come from the invocation of the last
delegate in the list. If an exception occurs during processing of the
invocation of such a delegate, and that
exception is not caught within the method that was invoked, the search for
an exception catch clause continues in
the method that called the delegate, and any methods further down the
invocation list are not invoked.
Attempting to invoke a delegate instance whose value is null results in an
exception of type
System.NullReferenceException.
[Example: The following example shows how to instantiate, combine, remove,
and invoke delegates:
C# LANGUAGE SPECIFICATION
300
using System;
delegate void D(int x);
class Test
{
public static void M1(int i) {
Console.WriteLine("Test.M1: " + i);
}
public static void M2(int i) {
Console.WriteLine("Test.M2: " + i);
}
public void M3(int i) {
Console.WriteLine("Test.M3: " + i);
}
}
class Demo
{
static void Main() {
D cd1 = new D(Test.M1);
cd1(-1); // call M1
D cd2 = new D(Test.M2);
cd2(-2); // call M2
D cd3 = cd1 + cd2;
cd3(10); // call M1 then M2
cd3 += cd1;
cd3(20); // call M1, M2, then M1
Test t = new Test();
D cd4 = new D(t.M3);
cd3 += cd4;
cd3(30); // call M1, M2, M1, then M3
cd3 -= cd1; // remove last M1
cd3(40); // call M1, M2, then M3
cd3 -= cd4;
cd3(50); // call M1 then M2
cd3 -= cd2;
cd3(60); // call M1
cd3 -= cd2; // impossible removal is benign
cd3(60); // call M1
cd3 -= cd1; // invocation list is empty
// cd3(70); // System.NullReferenceException thrown
cd3 -= cd1; // impossible removal is benign
}
}
As shown in the statement cd3 += cd1;, a delegate can be present in an
invocation list multiple times. In this
case, it is simply invoked once per occurrence. In an invocation list such
as this, when that delegate is removed,
the last occurrence in the invocation list is the one actually removed.
Immediately prior to the execution of the final statement, cd3 -= cd1;, the
delegate cd3 refers to an empty
invocation list. Attempting to remove a delegate from an empty list (or to
remove a non-existent delegate from a
non-empty list) is not an error.
The output produced is:
Test.M1: -1
Test.M2: -2
Test.M1: 10
Test.M2: 10
Test.M1: 20
Test.M2: 20
Test.M1: 20
Chapter 22 Delegates
301
Test.M1: 30
Test.M2: 30
Test.M1: 30
Test.M3: 30
Test.M1: 40
Test.M2: 40
Test.M3: 40
Test.M1: 50
Test.M2: 50
Test.M1: 60
Test.M1: 60
end example]

拦截器(Interceptor)中的invocation.invoke()是什么意思?

关于在Struts2的自定义的验证拦截器(Interceptor)中的invocation.invoke()是什么意思? package com.xjtu.interceptor; imp...
  • canlets
  • canlets
  • 2013年08月16日 16:08
  • 9268

mybatis -- 由浅入深分析mybatis通过动态代理实现拦截器(插件)的原理

转载自: http://zhangbo-peipei-163-com.iteye.com/blog/2033832 最近在用mybatis做项目,需要用到mybatis的拦截器功能,就顺便把...
  • luojinbai
  • luojinbai
  • 2016年07月11日 22:37
  • 466

iOS中的delegate的用法和规范

Objective-C中的delegatedelegate是Objective-C编写的应用中各种对象之间互相调用的主要方式之一. 通常情况下, 对象可以接收的消息都通过在接口文件中声明的方法来表示....
  • uxyheaven
  • uxyheaven
  • 2015年09月07日 09:56
  • 21177

使用Activity和Delegate与适配器交互

在这个Demo里,我们会分析如何通过委托模式(Delegation Pattern)帮助开发者把所有的业务逻辑从适配器中移到Activity中。我们创建一个简单的应用程序,通过该应用程序可以向列表中添...
  • tw19911005
  • tw19911005
  • 2015年12月19日 16:04
  • 1219

Hive自定义UDAF详解

Hive自定义UDAF详解         遇到一个Hive需求:有A、B、C三列,按A列进行聚合,求出C列聚合后的最小值和最大值各自对应的B列值。这个需求用hql和...
  • q412774506
  • q412774506
  • 2015年08月17日 09:42
  • 484

delegate协议实现一对多

代理一般是一对一的, 如果要一对多, 那么就是多播代理; 多播代理只是多了个中间层,但最终遵守协议的只有一个, 那就是中间层, 所以不是 delegates, 依然是 delegate 假...
  • ygh5123687
  • ygh5123687
  • 2017年05月20日 15:15
  • 683

终于会用c#中的delegate(委托)

一、开篇忏悔 对自己最拿手的编程语言C#,我想对你说声对不起,因为我到现在为止才明白c#中的delegate和event是怎么用的,惭愧那。好了,那今天就趁月黑风高的夜晚简单来谈谈delegate和e...
  • agilelau
  • agilelau
  • 2015年01月21日 09:35
  • 1965

C#中的委托(Delegate)和事件(Event)

分类:   把C#中的委托(Delegate)和事件(Event)放到现在讲是有目的的:给下次写的设计模式——观察者(Observer)有一个参考。     委托和事件应该是C#相较于...
  • haihsl123456789
  • haihsl123456789
  • 2015年03月13日 22:45
  • 954

java反射之dynamic invocation与原生类型

java反射之dynamic invocation与原生类型 java中的方法参数或者返回值可能为原生类型,反射调用方法的时候,理解在反射invoke方法中如何使用原生类型很重要。        ...
  • doctor_who2004
  • doctor_who2004
  • 2016年08月28日 18:15
  • 1038

java中设计模式-delegate(代理)

http://yangguangfu.iteye.com/blog/815787---这个是很好的参考 关于代理的一个例子:开会--老板--秘书小明/开会--CEO--秘书小明,小明可以代替...
  • shb2058
  • shb2058
  • 2016年12月27日 14:30
  • 1830
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:22.3 Delegate invocation
举报原因:
原因补充:

(最多只允许输入30个字)