共用方式為


MulticastDelegate Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Represents a multicast delegate; that is, a delegate that can have more than one element in its invocation list.

Inheritance Hierarchy

System.Object
  System.Delegate
    System.MulticastDelegate

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public MustInherit Class MulticastDelegate _
    Inherits Delegate
[ComVisibleAttribute(true)]
public abstract class MulticastDelegate : Delegate

The MulticastDelegate type exposes the following members.

Constructors

  Name Description
Protected method MulticastDelegate Initializes a new instance of the MulticastDelegate class.

Top

Properties

  Name Description
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Method Gets the method represented by the delegate. (Inherited from Delegate.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Target Gets the class instance on which the current delegate invokes the instance method. (Inherited from Delegate.)

Top

Methods

  Name Description
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 CombineImpl Combines this Delegate with the specified Delegate to form a new delegate. (Overrides Delegate.CombineImpl(Delegate).)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 DynamicInvoke Dynamically invokes (late-bound) the method represented by the current delegate. (Inherited from Delegate.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals Determines whether this multicast delegate and the specified object are equal. (Overrides Delegate.Equals(Object).)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)

In Silverlight for Windows Phone, this member is overridden by Finalize().

In XNA Framework, this member is overridden by Finalize().
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Returns the hash code for this instance. (Overrides Delegate.GetHashCode().)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetInvocationList Returns the invocation list of this multicast delegate, in invocation order. (Overrides Delegate.GetInvocationList().)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 RemoveImpl Removes an element from the invocation list of this MulticastDelegate that is equal to the specified delegate. (Overrides Delegate.RemoveImpl(Delegate).)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Operators

  Name Description
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equality Determines whether two MulticastDelegate objects are equal.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Inequality Determines whether two MulticastDelegate objects are not equal.

Top

Remarks

MulticastDelegate is a special class. Compilers and other tools can derive from this class, but you cannot derive from it explicitly. The same is true of the Delegate class.

In Silverlight, using delegates to make asynchronous method calls is not supported. Calling BeginInvoke causes a NotSupportedException.

A MulticastDelegate has a linked list of delegates, called an invocation list, consisting of one or more elements. When a multicast delegate is invoked, the delegates in the invocation list are called synchronously in the order in which they appear. If an error occurs during execution of the list then an exception is thrown.

Examples

The following example demonstrates the use of the MulticastDelegate class.

' Declare a delegate class named CheckAndPrintDelegate that takes a
' string. All delegates derive from System.MulticastDelegate.
Delegate Sub CheckAndPrintDelegate(ByVal str As String)

' This class demonstrates using Delegates, including using the Remove and
' Combine methods to create and modify delegate combinations. 
'
Class Example

   Private Shared outputBlock As System.Windows.Controls.TextBlock

   ' A List that holds strings.
   Private Shared strList As New System.Collections.Generic.List(Of String)

   ' Iterate through the strings and invoke the method(s) that the delegate 
   ' represents.
   Public Shared Sub PrintAllQualified(ByVal myDelegate As CheckAndPrintDelegate)
      For Each str As String In strList
         myDelegate(str)
      Next str
   End Sub 

   ' This method prints the string that is passed to it if the string 
   ' starts with a consonant.
   Public Shared Sub ConStart(ByVal str As String)
      If "aeiou".IndexOf(str.Chars(0)) < 0 Then 
         outputBlock.Text &= str & vbLf
      End If
   End Sub 

   ' This method prints the string that is passed to it if the string
   ' starts with a vowel.
   Public Shared Sub VowelStart(ByVal str As String)
      If "aeiou".IndexOf(str.Chars(0)) > -1 Then 
         outputBlock.Text &= str & vbLf
      End If
   End Sub 

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Example.outputBlock = outputBlock

      strList.Add("this")
      strList.Add("is")
      strList.Add("a")
      strList.Add("multicast")
      strList.Add("delegate")
      strList.Add("example")

      ' Create two delegates individually.
      Dim ConStartDel As New CheckAndPrintDelegate(AddressOf Example.ConStart)

      Dim VowStartDel As New CheckAndPrintDelegate(AddressOf Example.VowelStart)

      ' GetInvocationList() returns an array of all delegates stored in the 
      ' linked list of the MulticastDelegate. In these cases the lists will 
      ' hold only one delegate.
      outputBlock.Text &= "ConStartDel contains " _
         & ConStartDel.GetInvocationList().Length & " delegate(s)." & vbLf

      outputBlock.Text &= "VowStartDel contains " & _
         VowStartDel.GetInvocationList().Length & " delegate(s)." & vbLf

      ' Determine whether the delegates are System.Multicast delegates
      If TypeOf ConStartDel Is System.MulticastDelegate And _
         TypeOf VowStartDel Is System.MulticastDelegate Then

         outputBlock.Text &= "ConStartDel and VowStartDel are System.MulticastDelegate." & vbLf
      End If

      ' Run the two single delegates one after the other.
      outputBlock.Text &= "Using ConStartDel delegate:" & vbLf
      PrintAllQualified(ConStartDel)

      outputBlock.Text &= vbLf & "Using VowStartDel delegate:" & vbLf
      PrintAllQualified(VowStartDel)

      ' Delegate.Combine receives a list of MulticastDelegates as parameters 
      ' and combines their invocation lists in a new Multicast delegate.
      Dim MultiDel As CheckAndPrintDelegate = _
         CType([Delegate].Combine(ConStartDel, VowStartDel), CheckAndPrintDelegate)

      ' How many delegates is this delegate holding?
      outputBlock.Text &= vbLf & "MultiDel contains " & _
         MultiDel.GetInvocationList().Length & " delegates." & vbLf

      ' What happens when this mulitcast delegate is passed to 
      ' PrintAllQualified?
      outputBlock.Text &= "Using the Multicast delegate that combines ConStartDel and VowStartDel:" & vbLf
      PrintAllQualified(MultiDel)

      ' The Remove and Combine methods create new Multicast delegates with
      ' the specified delegates removed from or combined with the existing
      ' invocation list. 
      MultiDel = CType([Delegate].Remove(MultiDel, VowStartDel), CheckAndPrintDelegate)
      MultiDel = CType([Delegate].Combine(MultiDel, ConStartDel), CheckAndPrintDelegate)

      ' Pass the new Multicast delegate to PrintAllQualified.
      outputBlock.Text &= vbLf & "Using the Multicast delegate that contains two copies of ConStartDel:" & vbLf
      PrintAllQualified(MultiDel)

   End Sub 
End Class 

' This example produces the following output:
'
'ConStartDel contains 1 delegate(s).
'VowStartDel contains 1 delegate(s).
'ConStartDel and VowStartDel are System.MulticastDelegate.
'Using ConStartDel delegate:
'this
'multicast
'delegate
'
'Using VowStartDel delegate:
'is
'a
'example
'
'Using the Multicast delegate that combines ConStartDel and VowStartDel:
'this
'is
'a
'multicast
'delegate
'example
'
'Using the Multicast delegate that contains two copies of ConStartDel:
'this
'this
'multicast
'multicast
'delegate
'delegate
using System;

// Declare a delegate class named CheckAndPrintDelegate that takes a
// string. All delegates derive from System.MulticastDelegate.
public delegate void CheckAndPrintDelegate(string str);

// This class demonstrates using Delegates, including using the Remove and
// Combine methods to create and modify delegate combinations. 
//
class Example
{
   private static System.Windows.Controls.TextBlock outputBlock;

   // A List that holds strings.
   private static System.Collections.Generic.List<string> strList = 
      new System.Collections.Generic.List<string>();

   // Iterate through the strings and invoke the method(s) that the delegate 
   // represents.
   public static void PrintAllQualified(CheckAndPrintDelegate myDelegate)
   {
      foreach (string str in strList)
      {
         myDelegate(str);
      }
   }

   // This method prints the string that is passed to it if the string 
   // starts with a consonant.
   public static void ConStart(string str)
   {
      if ("aeiou".IndexOf(str[0]) < 0)
         outputBlock.Text += str + "\n";
   }

   // This method prints the string that is passed to it if the string
   // starts with a vowel.
   public static void VowelStart(string str)
   {
      if ("aeiou".IndexOf(str[0]) > -1)
         outputBlock.Text += str + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Example.outputBlock = outputBlock;

      strList.Add("this");
      strList.Add("is");
      strList.Add("a");
      strList.Add("multicast");
      strList.Add("delegate");
      strList.Add("example");

      // Create two delegates individually.
      CheckAndPrintDelegate ConStartDel =
          new CheckAndPrintDelegate(Example.ConStart);

      CheckAndPrintDelegate VowStartDel =
          new CheckAndPrintDelegate(Example.VowelStart);

      // GetInvocationList() returns an array of all delegates stored in the 
      // linked list of the MulticastDelegate. In these cases the lists will 
      // hold only one delegate.
      outputBlock.Text += "ConStartDel contains " + 
         ConStartDel.GetInvocationList().Length + " delegate(s)." + "\n";
      outputBlock.Text += "VowStartDel contains " + 
         VowStartDel.GetInvocationList().Length + " delegate(s)." + "\n";

      // Determine whether the delegates are System.Multicast delegates
      if (ConStartDel is System.MulticastDelegate && 
          VowStartDel is System.MulticastDelegate) 
      {
         outputBlock.Text += "ConStartDel and VowStartDel are System.MulticastDelegate." + "\n";
      }

      // Run the two single delegates one after the other.
      outputBlock.Text += "Using ConStartDel delegate:\n";
      PrintAllQualified(ConStartDel);

      outputBlock.Text += "\nUsing VowStartDel delegate:\n";
      PrintAllQualified(VowStartDel);

      // Delegate.Combine receives a list of MulticastDelegates as parameters 
      // and combines their invocation lists in a new Multicast delegate.
      CheckAndPrintDelegate MultiDel =
         (CheckAndPrintDelegate)Delegate.Combine(ConStartDel, VowStartDel);

      // How many delegates is this delegate holding?
      outputBlock.Text += "\nMultiDel contains " + 
         MultiDel.GetInvocationList().Length + " delegates." + "\n";

      // What happens when this mulitcast delegate is passed to 
      // PrintAllQualified?
      outputBlock.Text += "Using the Multicast delegate that combines ConStartDel and VowStartDel:" + "\n";
      PrintAllQualified(MultiDel);

      // The Remove and Combine methods create new Multicast delegates with
      // the specified delegates removed from or combined with the existing
      // invocation list. 
      MultiDel = (CheckAndPrintDelegate)Delegate.Remove(MultiDel, VowStartDel);
      MultiDel = (CheckAndPrintDelegate)Delegate.Combine(MultiDel, ConStartDel);

      // Pass the new Multicast delegate to PrintAllQualified.
      outputBlock.Text += "\nUsing the Multicast delegate that contains two copies of ConStartDel:" + "\n";
      PrintAllQualified(MultiDel);
   }  
}   

/* This example produces the following output:

ConStartDel contains 1 delegate(s).
VowStartDel contains 1 delegate(s).
ConStartDel and VowStartDel are System.MulticastDelegate.
Using ConStartDel delegate:
this
multicast
delegate

Using VowStartDel delegate:
is
a
example

Using the Multicast delegate that combines ConStartDel and VowStartDel:
this
is
a
multicast
delegate
example

Using the Multicast delegate that contains two copies of ConStartDel:
this
this
multicast
multicast
delegate
delegate
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference