MulticastDelegate多路广播委托

MulticastDelegate

表示多路广播委托;即,其调用列表中可以拥有多个元素的委托。

继承层次结构
System.Object
   System.Delegate
     System.MulticastDelegate
       派生类


备注

MulticastDelegate 是一个特殊类。编译器和其他工具可以从此类派生,但是您不能显式地从此类进行派生。Delegate 类也是如此。

MulticastDelegate 拥有一个带有链接的委托列表,该列表称为调用列表,它包含一个或多个元素。在调用多路广播委托时,将按照调用列表中的委托出现的顺序来同步调用这些委托。如果在该列表的执行过程中发生错误,则会引发异常。

Delegate 方法
Combine 已重载。 将指定的多路广播(可组合)委托的调用列表连接起来。
GetInvocationList 返回委托的调用列表。
MulticastDelegate 方法
Remove  从一个委托的调用列表中移除另一个委托的最后一个调用列表。 (从 Delegate 继承。)

示例
using  System;

    
//  This class contains strings. It has a member method that
    
//  accepts a multicast delegate as a parameter and calls it.

    
class  HoldsStrings
    {
        
//  The following line causes the compiler to generate
        
//  a new delegate class named CheckAndPrintDelegate that
        
//  inherits from System.MulticastDelegate.
         public   delegate   void  CheckAndPrintDelegate( string  str);

        
//  An ArrayList that holds strings
         private  System.Collections.ArrayList myStringArray  =   new  System.Collections.ArrayList();

        
//  A method that adds more strings to the Collection
         public   void  addstring(  string  str) {
            myStringArray.Add(str);
        }

        
//  Iterate through the strings and invoke the method(s) that the delegate points to
         public   void  PrintAllQualified(CheckAndPrintDelegate myDelegate) {
            
foreach  ( string  str  in  myStringArray) {
                myDelegate(str);
            }
        }
    }   
// end of class HoldsStrings

    
//  This class contains a few sample methods
     class  StringFuncs
    {
        
//  This method prints a string that it is passed if the string starts with a vowel
         public   static   void  ConStart( string  str) {
            
if  ( ! (str[ 0 ] == ' a ' || str[ 0 ] == ' e ' || str[ 0 ] == ' i ' || str[ 0 ] == ' o ' || str[ 0 ] == ' u ' ))
                Console.WriteLine(str);
        }

        
//  This method prints a string that it is passed if the string starts with a consonant
         public   static   void  VowelStart( string  str) {
            
if  ((str[ 0 ] == ' a ' || str[ 0 ] == ' e ' || str[ 0 ] == ' i ' || str[ 0 ] == ' o ' || str[ 0 ] == ' u ' ))
                Console.WriteLine(str);
        }
    }

    
//  This class demonstrates using Delegates, including using the Remove and
    
//  Combine methods to create and modify delegate combinations.
     class  Test
    {
        
static   public   void  Main()
        {
            
//  Declare the HoldsStrings class and add some strings
            HoldsStrings myHoldsStrings  =   new  HoldsStrings();
            myHoldsStrings.addstring(
" This " );
            myHoldsStrings.addstring(
" is " );
            myHoldsStrings.addstring(
" a " );
            myHoldsStrings.addstring(
" multicast " );
            myHoldsStrings.addstring(
" delegate " );
            myHoldsStrings.addstring(
" example " );

            
//  Create two delegates individually using different methods
            HoldsStrings.CheckAndPrintDelegate ConStartDel  =
                
new  HoldsStrings.CheckAndPrintDelegate(StringFuncs.ConStart);
            HoldsStrings.CheckAndPrintDelegate VowStartDel 
=
                
new  HoldsStrings.CheckAndPrintDelegate(StringFuncs.VowelStart);

            
//  Demonstrate that MulticastDelegates may store only one delegate
            Delegate [] DelegateList;

            
//  Returns an array of all delegates stored in the linked list of the
            
//  MulticastDelegate. In these cases the lists will hold only one (Multicast) delegate
            DelegateList  =  ConStartDel.GetInvocationList();
            Console.WriteLine(
" ConStartDel contains  "   +  DelegateList.Length  +   "  delegate(s). " );
            DelegateList 
=  VowStartDel.GetInvocationList();
            Console.WriteLine(
" ConStartVow contains  "   +  DelegateList.Length  +   "  delegate(s). " );

            
//  Determine whether the delegates are System.Multicast delegates
            
//  if (ConStartDel is System.MulticastDelegate && VowStartDel is System.MulticastDelegate) {
                Console.WriteLine( " ConStartDel and ConStartVow are System.MulticastDelegates " );
            
//  }

            
//  Run the two single delegates one after the other
            Console.WriteLine( " Running ConStartDel delegate: " );
            myHoldsStrings.PrintAllQualified(ConStartDel);
            Console.WriteLine(
" Running VowStartDel delegate: " );
            myHoldsStrings.PrintAllQualified(VowStartDel);

            
//  Create a new, empty MulticastDelegate
            HoldsStrings.CheckAndPrintDelegate MultiDel;

            
//  Delegate.Combine receives an unspecified number of MulticastDelegates as parameters
            MultiDel  =  (HoldsStrings.CheckAndPrintDelegate) Delegate.Combine(ConStartDel, VowStartDel);

            
//  How many delegates is this delegate holding?
            DelegateList  =  MultiDel.GetInvocationList();
            Console.WriteLine(
" \nMulitDel contains  "   +  DelegateList.Length  +   "  delegates. " );

            
//  What happens when this mulitcast delegate is passed to PrintAllQualified
            Console.WriteLine( " Running the multiple delegate that combined the first two " );
            myHoldsStrings.PrintAllQualified(MultiDel);

            
//  The Remove and Combine methods modify the multiple delegate
            MultiDel  =  (HoldsStrings.CheckAndPrintDelegate) Delegate.Remove(MultiDel, VowStartDel);
            MultiDel 
=  (HoldsStrings.CheckAndPrintDelegate) Delegate.Combine(MultiDel, ConStartDel);

            
//  Finally, pass the combined delegates to PrintAllQualified again
            Console.WriteLine( " \nRunning the multiple delegate that contains two copies of ConStartDel: " );
            myHoldsStrings.PrintAllQualified(MultiDel);

            
return ;
        }   
// end of main
    }    // end of Test

转载于:https://www.cnblogs.com/jdmei520/archive/2009/08/18/1549123.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值