Export (0) Print
Expand All
3 out of 4 rated this helpful - Rate this topic

MulticastDelegate Class

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

System.Object
  System.Delegate
    System.MulticastDelegate

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class MulticastDelegate : Delegate

The MulticastDelegate type exposes the following members.

  NameDescription
Protected methodMulticastDelegate(Object, String)Initializes a new instance of the MulticastDelegate class.
Protected methodMulticastDelegate(Type, String)Initializes a new instance of the MulticastDelegate class.
Top
  NameDescription
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryMethodGets the method represented by the delegate. (Inherited from Delegate.)
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryTargetGets the class instance on which the current delegate invokes the instance method. (Inherited from Delegate.)
Top
  NameDescription
Public methodSupported by the XNA FrameworkCloneCreates a shallow copy of the delegate. (Inherited from Delegate.)
Protected methodSupported by the XNA FrameworkCombineImplCombines this Delegate with the specified Delegate to form a new delegate. (Overrides Delegate.CombineImpl(Delegate).)
Public methodSupported by Portable Class LibraryDynamicInvokeDynamically invokes (late-bound) the method represented by the current delegate. (Inherited from Delegate.)
Protected methodDynamicInvokeImplDynamically invokes (late-bound) the method represented by the current delegate. (Inherited from Delegate.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryEqualsDetermines whether this multicast delegate and the specified object are equal. (Overrides Delegate.Equals(Object).)
Protected methodSupported by the XNA FrameworkFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)

In XNA Framework, this member is overridden by Finalize().
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetHashCodeReturns the hash code for this instance. (Overrides Delegate.GetHashCode().)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetInvocationListReturns the invocation list of this multicast delegate, in invocation order. (Overrides Delegate.GetInvocationList().)
Protected methodGetMethodImplReturns a static method represented by the current MulticastDelegate. (Overrides Delegate.GetMethodImpl().)
Public methodGetObjectDataPopulates a SerializationInfo object with all the data needed to serialize this instance. (Overrides Delegate.GetObjectData(SerializationInfo, StreamingContext).)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkRemoveImplRemoves an element from the invocation list of this MulticastDelegate that is equal to the specified delegate. (Overrides Delegate.RemoveImpl(Delegate).)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryToStringReturns a string that represents the current object. (Inherited from Object.)
Top
  NameDescription
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryEqualityDetermines whether two MulticastDelegate objects are equal.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryInequalityDetermines whether two MulticastDelegate objects are not equal.
Top

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.

The following example demonstrates the use of a class that is derived from MulticastDelegate.


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


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.