次の方法で共有


MulticastDelegate クラス

マルチキャスト デリゲート、つまり呼び出しリストに複数の要素を組み込むことができるデリゲートを表します。

この型のすべてのメンバの一覧については、MulticastDelegate メンバ を参照してください。

System.Object
   System.Delegate
      System.MulticastDelegate
         派生クラス

<Serializable>
MustInherit Public Class MulticastDelegate   Inherits Delegate
[C#]
[Serializable]
public abstract class MulticastDelegate : Delegate
[C++]
[Serializable]
public __gc __abstract class MulticastDelegate : public Delegate
[JScript]
public
   Serializable
abstract class MulticastDelegate extends Delegate

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

MulticastDelegate は特殊なクラスです。コンパイラおよびその他のツールはこのクラスから派生させることができますが、明示的に派生させることはできません。これは Delegate クラスにも当てはまります。

MulticastDelegate には、複数の関連するデリゲートをまとめたリストがあり、このリストは呼び出しリストと呼ばれ、1 つ以上の要素で構成されます。マルチキャスト デリゲートが呼び出されると、同時に呼び出しリストのデリゲートが出現順に呼び出されます。呼び出しリストの実行中にエラーが発生すると、例外がスローされます。

使用例

[Visual Basic, C#, C++] MulticastDelegate から派生したクラスを使用する例を次に示します。

 
Imports System
Imports Microsoft.VisualBasic

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

Class HoldsStrings

    ' This delegate is declared as a void, so the compiler automatically
    ' generates a new class, named CheckAndPrintDelegate, that inherits from
    ' System.MulticastDelegate.
    ' If this delegate were declared with a Boolean return value, it
    ' would inherit from System.Delegate and be a singlecast delegate.
    Delegate Sub CheckAndPrintDelegate(ByVal str As String)

    ' An ArrayList that holds strings
    Private myStringArray As New System.Collections.ArrayList()

    Public Sub addstring(ByVal str As String)
        myStringArray.Add(str)
    End Sub 'addstring

    ' Iterate through the strings and invoke the method(s) that the delegate points to
    Public Sub PrintAllQualified(ByVal myDelegate As CheckAndPrintDelegate)
        Dim str As String
        For Each str In myStringArray
            myDelegate(str)
        Next str
    End Sub 'PrintAllQualified

End Class     '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 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 'ConStart

    ' This method prints a string that it is passed if the string starts with a consonant
    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 'VowelStart
End Class 'StringFuncs

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

    Public Shared Sub Main()
        ' Declare the HoldsStrings class and add some strings
        Dim myHoldsStrings As 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
        Dim ConStartDel = New HoldsStrings.CheckAndPrintDelegate(AddressOf StringFuncs.ConStart)

        Dim VowStartDel As New HoldsStrings.CheckAndPrintDelegate(AddressOf StringFuncs.VowelStart)

        ' Demonstrate that MulticastDelegates may store only one delegate
        Dim DelegateList() As [Delegate]

        ' 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.ToString() + " delegate(s).")

        DelegateList = VowStartDel.GetInvocationList()
        Console.WriteLine(("ConStartVow contains " + DelegateList.Length.ToString() + " delegate(s)."))

        ' Determine whether the delegates are System.Multicast delegates
        If TypeOf ConStartDel Is System.MulticastDelegate And TypeOf VowStartDel Is System.MulticastDelegate Then
            Console.WriteLine("ConStartDel and ConStartVow are System.MulticastDelegates")
        End If

        ' 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
        Dim MultiDel As HoldsStrings.CheckAndPrintDelegate

        ' Delegate.Combine receives an unspecified number of MulticastDelegates as parameters
        MultiDel = CType([Delegate].Combine(ConStartDel, VowStartDel), HoldsStrings.CheckAndPrintDelegate)

        ' How many delegates is this delegate holding?
        DelegateList = MultiDel.GetInvocationList()
        Console.WriteLine((ControlChars.Cr + "MulitDel contains " + DelegateList.Length.ToString() + " 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 = CType([Delegate].Remove(MultiDel, VowStartDel), HoldsStrings.CheckAndPrintDelegate)
        MultiDel = CType([Delegate].Combine(MultiDel, ConStartDel), HoldsStrings.CheckAndPrintDelegate)

        ' Finally, pass the combined delegates to PrintAllQualified again
        Console.WriteLine(ControlChars.Cr + "Running the multiple delegate that contains two copies of ConStartDel:")
        myHoldsStrings.PrintAllQualified(MultiDel)

        Return
    End Sub 'end of main
End Class 'end of Test


[C#] 
using System;

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

    class HoldsStrings
    {
        // This delegate is declared as a void, so the compiler automatically
        // generates a new class, named CheckAndPrintDelegate, that inherits from
        // System.MulticastDelegate.
        // If this delegate were declared with a Boolean return value, it
        // would inherit from System.Delegate and be a singlecast delegate.
        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

[C++] 
#using <mscorlib.dll>

using namespace System;

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

__gc class HoldsStrings {
   // This delegate is declared as a void, so the compiler automatically
   // generates a new class, named CheckAndPrintDelegate, that inherits from
   // System::MulticastDelegate::
   // If this delegate were declared with a Boolean return value, it
   // would inherit from System::Delegate and be a singlecast delegate.
public:
   __delegate void CheckAndPrintDelegate(String* str);

   // An ArrayList that holds strings
private:
   System::Collections::ArrayList* myStringArray;

public:
   HoldsStrings() {
      myStringArray = new System::Collections::ArrayList();
   };

   // A method that adds more strings to the Collection
   void addstring(String* str) {
      myStringArray->Add(str);
   };

   // Iterate through the strings and invoke the method(s) that the delegate points to
   void PrintAllQualified(CheckAndPrintDelegate* myDelegate) {
      System::Collections::IEnumerator* myEnum = myStringArray->GetEnumerator();
      while (myEnum->MoveNext()) {
         String* str = __try_cast<String*>(myEnum->Current);
         myDelegate(str);
      };
   };
};   //end of class HoldsStrings

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

   // This method prints a String* that it is passed if the String* starts with a consonant
   static void VowelStart(String* str) {
      if ((str->Chars[0]=='a'||str->Chars[0]=='e'||str->Chars[0]=='i'||str->Chars[0]=='o'||str->Chars[0]=='u'))
         Console::WriteLine(str);
   }
};

// This function demonstrates using Delegates, including using the Remove and
// Combine methods to create and modify delegate combinations.
int main() {
   // Declare the HoldsStrings class and add some strings
   HoldsStrings* myHoldsStrings = new HoldsStrings();
   myHoldsStrings->addstring(S"This");
   myHoldsStrings->addstring(S"is");
   myHoldsStrings->addstring(S"a");
   myHoldsStrings->addstring(S"multicast");
   myHoldsStrings->addstring(S"delegate");
   myHoldsStrings->addstring(S"example");

   // Create two delegates individually using different methods
   HoldsStrings::CheckAndPrintDelegate* ConStartDel = new HoldsStrings::CheckAndPrintDelegate(0, StringFuncs::ConStart);
   HoldsStrings::CheckAndPrintDelegate* VowStartDel = new HoldsStrings::CheckAndPrintDelegate(0, 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(S"ConStartDel contains {0} delegate(s).", __box( DelegateList->Length));
   DelegateList = VowStartDel->GetInvocationList();
   Console::WriteLine(S"ConStartVow contains {0} delegate(s).", __box( DelegateList->Length));
   // Determine whether the delegates are System::Multicast delegates

   if (dynamic_cast<System::MulticastDelegate *>(ConStartDel) && dynamic_cast<System::MulticastDelegate*>(VowStartDel)) {
      Console::WriteLine(S"ConStartDel and ConStartVow are System::MulticastDelegates");
   }

   // Run the two single delegates one after the other
   Console::WriteLine(S"Running ConStartDel delegate:");
   myHoldsStrings->PrintAllQualified(ConStartDel);
   Console::WriteLine(S"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 = dynamic_cast<HoldsStrings::CheckAndPrintDelegate*> (Delegate::Combine(ConStartDel, VowStartDel));

   // How many delegates is this delegate holding?
   DelegateList = MultiDel->GetInvocationList();
   Console::WriteLine(S"\nMulitDel contains {0} delegates.", __box( DelegateList->Length));

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

   // The Remove and Combine methods modify the multiple delegate
   MultiDel = dynamic_cast<HoldsStrings::CheckAndPrintDelegate*> (Delegate::Remove(MultiDel, VowStartDel));
   MultiDel = dynamic_cast<HoldsStrings::CheckAndPrintDelegate*> (Delegate::Combine(MultiDel, ConStartDel));

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

}   //end of main

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

MulticastDelegate メンバ | System 名前空間