Läs på engelska Redigera

Dela via


MulticastDelegate Class

Definition

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

public abstract class MulticastDelegate : Delegate
[System.Serializable]
public abstract class MulticastDelegate : Delegate
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class MulticastDelegate : Delegate
Inheritance
MulticastDelegate
Attributes

Examples

The following example defines a class, StringContainer, which includes a collection of strings. One of its members is the CheckAndDisplayDelegate delegate, which is used to display strings stored in a StringContainer object that satisfy particular criteria. The delegate takes a single string as a parameter and returns void (or, in Visual Basic, it's a Sub procedure). It also includes a method, DisplayAllQualified, that has a single parameter, a CheckAndDisplayDelegate delegate. This allows the method to be called and to display a set of strings that are filtered based on the methods that the delegate contains.

The example also defines a utility class, StringExtensions, that has two methods:

  • ConStart, which displays strings that begin with a consonant.

  • VowelStart, which displays strings that begin with a vowel.

Note that both methods include a single string parameter and return void. In other words, both methods can be assigned to the CheckAndDisplayDelegate delegate.

The Test.Main method is the application entry point. It instantiates a StringContainer object, populates it with strings, and creates two CheckAndDisplayDelegate delegates, conStart and vowelStart, that invoke a single method. It then calls the Delegate.Combine method to create the multipleDelegates delegate, which initially contains the ConStart and VowelStart delegates. Note that when the multipleDelegates delegate is invoked, it displays all the strings in the collection in their original order. This is because each letter is passed separately to each delegate, and each letter meets the filtering criteria of only one of the two delegates. Finally, after calls to Delegate.Remove and Delegate.Combine, multipleDelegates contains two conStart delegates. When it is invoked, each string in the StringContainer object is displayed twice.

using System;
using System.Collections.Generic;

class StringContainer
{
   // Define a delegate to handle string display.
   public delegate void CheckAndDisplayDelegate(string str);

   // A generic list object that holds the strings.
   private List<String> container = new List<String>();

   // A method that adds strings to the collection.
   public void AddString(string str)
   {
      container.Add(str);
   }

   // Iterate through the strings and invoke the method(s) that the delegate points to.
   public void DisplayAllQualified(CheckAndDisplayDelegate displayDelegate)
   {
      foreach (var str in container) {
         displayDelegate(str);
      }
   }
 }

// This class defines some methods to display strings.
class StringExtensions
{
   // Display a string if it starts with a consonant.
   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);
   }

   // Display a string if it starts with a vowel.
   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);
   }
}

// Demonstrate the use of delegates, including the Remove and
// Combine methods to create and modify delegate combinations.
class Test
{
   static public void Main()
   {
      // Declare the StringContainer class and add some strings
      StringContainer container = new StringContainer();
      container.AddString("This");
      container.AddString("is");
      container.AddString("a");
      container.AddString("multicast");
      container.AddString("delegate");
      container.AddString("example");

      // Create two delegates individually using different methods.
      StringContainer.CheckAndDisplayDelegate conStart = StringExtensions.ConStart;
      StringContainer.CheckAndDisplayDelegate vowelStart = StringExtensions.VowelStart;

      // Get the list of all delegates assigned to this MulticastDelegate instance.
      Delegate[] delegateList = conStart.GetInvocationList();
      Console.WriteLine("conStart contains {0} delegate(s).", delegateList.Length);
      delegateList = vowelStart.GetInvocationList();
      Console.WriteLine("vowelStart contains {0} delegate(s).\n", delegateList.Length);

      // Determine whether the delegates are System.Multicast delegates.
      if (conStart is System.MulticastDelegate && vowelStart is System.MulticastDelegate)
          Console.WriteLine("conStart and vowelStart are derived from MulticastDelegate.\n");

      // Execute the two delegates.
      Console.WriteLine("Executing the conStart delegate:");
      container.DisplayAllQualified(conStart);
      Console.WriteLine();
      Console.WriteLine("Executing the vowelStart delegate:");
      container.DisplayAllQualified(vowelStart);
      Console.WriteLine();

      // Create a new MulticastDelegate and call Combine to add two delegates.
      StringContainer.CheckAndDisplayDelegate multipleDelegates =
            (StringContainer.CheckAndDisplayDelegate) Delegate.Combine(conStart, vowelStart);

      // How many delegates does multipleDelegates contain?
      delegateList = multipleDelegates.GetInvocationList();
      Console.WriteLine("\nmultipleDelegates contains {0} delegates.\n",
                        delegateList.Length);

      // Pass this multicast delegate to DisplayAllQualified.
      Console.WriteLine("Executing the multipleDelegate delegate.");
      container.DisplayAllQualified(multipleDelegates);

      // Call remove and combine to change the contained delegates.
      multipleDelegates = (StringContainer.CheckAndDisplayDelegate) Delegate.Remove(multipleDelegates, vowelStart);
      multipleDelegates = (StringContainer.CheckAndDisplayDelegate) Delegate.Combine(multipleDelegates, conStart);

      // Pass multipleDelegates to DisplayAllQualified again.
      Console.WriteLine("\nExecuting the multipleDelegate delegate with two conStart delegates:");
      container.DisplayAllQualified(multipleDelegates);
   }
}
// The example displays the following output:
//    conStart contains 1 delegate(s).
//    vowelStart contains 1 delegate(s).
//
//    conStart and vowelStart are derived from MulticastDelegate.
//
//    Executing the conStart delegate:
//    This
//    multicast
//    delegate
//
//    Executing the vowelStart delegate:
//    is
//    a
//    example
//
//
//    multipleDelegates contains 2 delegates.
//
//    Executing the multipleDelegate delegate.
//    This
//    is
//    a
//    multicast
//    delegate
//    example
//
//    Executing the multipleDelegate delegate with two conStart delegates:
//    This
//    This
//    multicast
//    multicast
//    delegate
//    delegate

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 addition to the methods that delegate types inherit from MulticastDelegate, the common language runtime provides two special methods: BeginInvoke and EndInvoke. For more information about these methods, see Calling Synchronous Methods Asynchronously.

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.

Constructors

MulticastDelegate(Object, String)

Initializes a new instance of the MulticastDelegate class.

MulticastDelegate(Type, String)

Initializes a new instance of the MulticastDelegate class.

Properties

HasSingleTarget

Gets a value that indicates whether the Delegate has a single invocation target.

(Inherited from Delegate)
Method

Gets the method represented by the delegate.

(Inherited from Delegate)
Target

Gets the class instance on which the current delegate invokes the instance method.

(Inherited from Delegate)

Methods

Clone()

Creates a shallow copy of the delegate.

(Inherited from Delegate)
CombineImpl(Delegate)

Combines this Delegate with the specified Delegate to form a new delegate.

CombineImpl(Delegate)

Concatenates the invocation lists of the specified multicast (combinable) delegate and the current multicast (combinable) delegate.

(Inherited from Delegate)
DynamicInvoke(Object[])

Dynamically invokes (late-bound) the method represented by the current delegate.

(Inherited from Delegate)
DynamicInvokeImpl(Object[])

Processes the full invocation list.

DynamicInvokeImpl(Object[])

Dynamically invokes (late-bound) the method represented by the current delegate.

(Inherited from Delegate)
Equals(Object)

Determines whether this multicast delegate and the specified object are equal.

GetHashCode()

Returns the hash code for this instance.

GetInvocationList()

Returns the invocation list of this multicast delegate, in invocation order.

GetMethodImpl()

Returns a method represented by the current MulticastDelegate.

GetMethodImpl()

Gets the method represented by the current delegate.

(Inherited from Delegate)
GetObjectData(SerializationInfo, StreamingContext)
Obsolete.

Populates a SerializationInfo object with all the data needed to serialize this instance.

GetObjectData(SerializationInfo, StreamingContext)
Obsolete.

Not supported.

(Inherited from Delegate)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
RemoveImpl(Delegate)

Removes an element from the invocation list of this MulticastDelegate that is equal to the specified delegate.

RemoveImpl(Delegate)

Removes the invocation list of a delegate from the invocation list of another delegate.

(Inherited from Delegate)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Operators

Equality(MulticastDelegate, MulticastDelegate)

Determines whether two MulticastDelegate objects are equal.

Inequality(MulticastDelegate, MulticastDelegate)

Determines whether two MulticastDelegate objects are not equal.

Extension Methods

GetMethodInfo(Delegate)

Gets an object that represents the method represented by the specified delegate.

Applies to

Produkt Versioner
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0