Export (0) Print
Expand All

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)

'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public MustInherit Class MulticastDelegate _
	Inherits 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 LibrarySupported in .NET for Windows Store appsTargetGets 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 LibrarySupported in .NET for Windows Store appsDynamicInvokeDynamically 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 LibrarySupported in .NET for Windows Store appsEqualsDetermines 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 LibrarySupported in .NET for Windows Store appsGetHashCodeReturns the hash code for this instance. (Overrides Delegate.GetHashCode.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetInvocationListReturns 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 LibrarySupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsMemberwiseCloneCreates 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 LibrarySupported in .NET for Windows Store appsToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEqualityDetermines whether two MulticastDelegate objects are equal.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsInequalityDetermines whether two MulticastDelegate objects are not equal.
Top

  NameDescription
Public Extension MethodSupported in .NET for Windows Store appsGetMethodInfoGets an object that represents the method represented by the specified delegate. (Defined by RuntimeReflectionExtensions.)
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 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.

Imports System.Collections.Generic

Class StringContainer
    ' Define a delegate to handle string display.  
    Delegate Sub CheckAndPrintDelegate(ByVal str As String)

    ' A generic list object that holds the strings. 
    Private container As New List(Of String)()

     ' A method that adds strings to the collection.  
     Public Sub AddString(ByVal s As String)
        container.Add(s)
    End Sub 

    ' Iterate through the strings and invoke the method(s) that the delegate points to. 
    Public Sub DisplayAllQualified(ByVal displayDelegate As CheckAndPrintDelegate)
        For Each s In container
            displayDelegate(s)
        Next 
    End Sub 
End Class 

' This class defines some methods to display strings.  
Class StringExtensions
    ' Display a string if it starts with a consonant.  
    Public Shared Sub ConStart(ByVal str As String)
        If Not (str.Chars(0) = "a"c Or str.Chars(0) = "e"c Or str.Chars(0) = "i"c _ 
                Or str.Chars(0) = "o"c Or str.Chars(0) = "u"c) Then
            Console.WriteLine(str)
        End If 
    End Sub  

    ' Display a string if it starts with a vowel. 
    Public Shared Sub VowelStart(ByVal str As String)
        If (str.Chars(0) = "a"c Or str.Chars(0) = "e"c Or str.Chars(0) = "i"c _
            Or str.Chars(0) = "o"c Or str.Chars(0) = "u"c) Then
            Console.WriteLine(str)
        End If 
    End Sub  
End Class  

' Demonstrate the use of delegates, including the Remove and  
' Combine methods to create and modify delegate combinations.  
Class Test
    Public Shared Sub Main()
        ' Declare the StringContainer class and add some strings 
        Dim container As 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. 
        Dim constart As StringContainer.CheckAndPrintDelegate = AddressOf StringExtensions.ConStart
        Dim vowelStart As StringContainer.CheckAndPrintDelegate = AddressOf StringExtensions.VowelStart

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

        ' Determine whether the delegates are System.Multicast delegates 
        If TypeOf conStart Is System.MulticastDelegate And TypeOf vowelStart Is System.MulticastDelegate Then
            Console.WriteLine("conStart and vowelStart are derived from MulticastDelegate.")
            Console.WriteLine()
        End If 

        ' Run the two single delegates one after the other.
        Console.WriteLine("Executing the conStart delegate:")
        container.DisplayAllQualified(conStart)
        Console.WriteLine("Executing the vowelStart delegate:")
        container.DisplayAllQualified(vowelStart)
        Console.WriteLine()

        ' Create a new MulticastDelegate and call Combine to add two delegates. 
        Dim multipleDelegates As StringContainer.CheckAndPrintDelegate = 
                  CType([Delegate].Combine(conStart, vowelStart), 
                  StringContainer.CheckAndPrintDelegate)

        ' How many delegates does multipleDelegates contain?
        delegateList = multipleDelegates.GetInvocationList()
        Console.WriteLine("{1}multipleDelegates contains {0} delegates.{1}",
                          delegateList.Length, vbCrLf)

        ' Pass this mulitcast delegate to DisplayAllQualified.
        Console.WriteLine("Executing the multipleDelegate delegate.")
        container.DisplayAllQualified(multipleDelegates)

        ' Call remove and combine to change the contained delegates.
        multipleDelegates = CType([Delegate].Remove(multipleDelegates, vowelStart), 
                            StringContainer.CheckAndPrintDelegate)
        multipleDelegates = CType([Delegate].Combine(multipleDelegates, conStart), 
                            StringContainer.CheckAndPrintDelegate)

        ' Pass multipleDelegates to DisplayAllQualified again.
        Console.WriteLine()
        Console.WriteLine("Executing the multipleDelegate delegate with two conStart delegates:")
        container.DisplayAllQualified(multipleDelegates)
    End Sub  
End Class  
' 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

.NET Framework

Supported in: 4.5, 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

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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.
Show:
© 2014 Microsoft