《.Net框架程序设计》读书笔记 之 委托链

开始没记笔记,以后再补基础

:委托链
      委托的Invoke()方法,在委托链上,具有调用一个委托对象之前的委托对象(如果存在)的能力,这样,可以确保委托链上所有的委托对象都得到调用。但它的缺点是:只能取得最后一个调用对象的返回值,而前面调用对象的返回值都会丢失;另外,如果中间有一个委托对象产生异常或阻塞很长的时间,则会阻止其后面所有的委托对象的调用,因为委托链上的对象是按序调用的。
针对此种情况,MulticastDelegate类提供一个实例方法:GetInvocationList(),它返回一个委托形式的数组,从而可以任意调用委托链上的对象。

None.gif using  System;
None.gif
using  System.Text;
None.gif
None.gif
namespace  delegateStudy
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class aClass
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public string A()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"Write A");
InBlock.gif            
return "this is A";
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
class bClass
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{        
InBlock.gif        
public string B()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"Write B");
InBlock.gif            
return "this is B";
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
class cClass
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public string C()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine(
"Write C");
InBlock.gif            
return "this is C";
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
class delegateStudy
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{        
InBlock.gif        
delegate string GetReturnValue();  //定义
InBlock.gif
        static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//声明一个空委托
InBlock.gif
            GetReturnValue getReturn = null;
InBlock.gif            
//将上面三个方法添加到委托链上
InBlock.gif
            getReturn +=  new GetReturnValue(new aClass().A);
InBlock.gif            getReturn 
+=  new GetReturnValue(new bClass().B);
InBlock.gif            getReturn 
+=  new GetReturnValue(new cClass().C);           
InBlock.gif            Why(getReturn);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
static void Why(GetReturnValue grv)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//委托为空则什么事也不做
InBlock.gif
            if(grv == nullreturn;
InBlock.gif            
//获取一个由委托链上对象组成的数据,用GetInvocationList()方法
InBlock.gif
            Delegate[] arrayOfDelegate = grv.GetInvocationList();
InBlock.gif            
foreach(GetReturnValue getR in arrayOfDelegate)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(getR());                
ExpandedSubBlockEnd.gif            }

InBlock.gif              
//取得对应委托对象的回调方法名
InBlock.gif
            Console.WriteLine(arrayOfDelegate[2].Method);
InBlock.gif             
//输出对应委托对象回调方法属于哪个名称空间和类,这里输出为delegateStudy.bClass
InBlock.gif
            Console.WriteLine(arrayOfDelegate[1].Target);
InBlock.gif            
//调用arrayOfDelegate[0]位置委托对象的回调方法,传对应参数,没有参数为null
InBlock.gif            
//执行了aClass里的A()  
InBlock.gif
            Console.WriteLine(arrayOfDelegate[0].DynamicInvoke(null));
InBlock.gif            Console.Read();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/sunsjorlin/archive/2005/11/18/279539.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值