MulticastDelegate 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示多點傳送委派;也就是說,委派可以在它的引動過程清單中包含一個以上的項目。
public ref class MulticastDelegate abstract : Delegate
public abstract class MulticastDelegate : Delegate
[System.Serializable]
public abstract class MulticastDelegate : Delegate
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class MulticastDelegate : Delegate
type MulticastDelegate = class
inherit Delegate
[<System.Serializable>]
type MulticastDelegate = class
inherit Delegate
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type MulticastDelegate = class
inherit Delegate
Public MustInherit Class MulticastDelegate
Inherits Delegate
- 繼承
- 屬性
範例
下列範例會定義 類別, StringContainer
其中包含字串集合。 其中一個成員是 CheckAndDisplayDelegate
委派,用來顯示儲存在符合特定準則之物件中的 StringContainer
字串。 委派會採用單一字串做為參數,並在 Visual Basic 中傳回 void
(,或在 Visual Basic 中,它是 Sub
程式) 。 它也包含具有單一參數 CheckAndDisplayDelegate
、委派的方法 DisplayAllQualified
。 這可讓呼叫 方法,並顯示一組字串,這些字串會根據委派包含的方法進行篩選。
此範例也會定義具有兩種方法的公用程式類別 StringExtensions
:
ConStart
,其會顯示以同音開頭的字串。VowelStart
,其會顯示以 vowel 開頭的字串。
請注意,這兩種方法都包含單一字串參數並傳回 void
。 換句話說,這兩種方法都可以指派給 CheckAndDisplayDelegate
委派。
方法是 Test.Main
應用程式進入點。 它會具現化 StringContainer
物件、以字串填入物件,並建立兩 CheckAndDisplayDelegate
個委派, conStart
以及 vowelStart
叫用單一方法的 。 然後它會 Delegate.Combine 呼叫 方法來建立 multipleDelegates
委派,一開始包含 ConStart
和 VowelStart
委派。 請注意,叫用委派時 multipleDelegates
,它會以原始順序顯示集合中的所有字串。 這是因為每個字母會個別傳遞至每個委派,而且每個字母都只符合兩個委派之一的篩選準則。 最後,在 呼叫 Delegate.Remove 和 Delegate.CombinemultipleDelegates
之後,會包含兩 conStart
個委派。 叫用時,物件中的每個 StringContainer
字串都會顯示兩次。
using namespace System;
using namespace System::Collections::Generic;
ref class StringContainer
{
private:
// A generic list object that holds the strings.
List<String^>^ container = gcnew List<String^>;
public:
// Define a delegate to handle string display.
delegate void CheckAndDisplayDelegate(String^ str);
// A method that adds more strings to the collection.
void AddString(String^ str)
{
container->Add(str);
}
// Iterate through the strings and invoke the method(s) that the delegate points to.
void DisplayAllQualified(CheckAndDisplayDelegate^ displayDelegate)
{
for each (String^ str in container)
displayDelegate(str);
// System::Collections::IEnumerator^ myEnum = container->GetEnumerator();
// while ( myEnum->MoveNext() )
// {
// String^ str = safe_cast<String^>(myEnum->Current);
// displayDelegate(str);
// }
}
};
//end of class StringContainer
// This class contains a few sample methods
ref 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[ 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
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 function demonstrates using Delegates, including using the Remove and
// Combine methods to create and modify delegate combinations.
int main()
{
// Declare the StringContainer class and add some strings
StringContainer^ container = gcnew StringContainer;
container->AddString( "This" );
container->AddString( "is" );
container->AddString( "a" );
container->AddString( "multicast" );
container->AddString( "delegate" );
container->AddString( "example" );
// RETURN HERE.
// Create two delegates individually using different methods
StringContainer::CheckAndDisplayDelegate^ conStart = gcnew StringContainer::CheckAndDisplayDelegate( StringFuncs::ConStart );
StringContainer::CheckAndDisplayDelegate^ vowelStart = gcnew StringContainer::CheckAndDisplayDelegate( StringFuncs::VowelStart );
// Get the list of all delegates assigned to this MulticastDelegate instance.
array<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 ( dynamic_cast<System::MulticastDelegate^>(conStart) && dynamic_cast<System::MulticastDelegate^>(vowelStart) )
{
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);
// Create a new MulticastDelegate and call Combine to add two delegates.
StringContainer::CheckAndDisplayDelegate^ multipleDelegates =
dynamic_cast<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 = dynamic_cast<StringContainer::CheckAndDisplayDelegate^>
(Delegate::Remove(multipleDelegates, vowelStart));
multipleDelegates = dynamic_cast<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
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
module Test
open System
// Define a delegate to handle string display.
type CheckAndDisplayDelegate = delegate of string -> unit
type StringContainer() =
// A generic ResizeArray object that holds the strings.
let container = ResizeArray()
// A method that adds strings to the collection.
member _.AddString(str) =
container.Add str
// Iterate through the strings and invoke the method(s) that the delegate points to.
member _.DisplayAllQualified(displayDelegate: CheckAndDisplayDelegate) =
for str in container do
displayDelegate.Invoke str
// This module defines some functions to display strings.
module StringExtensions =
// Display a string if it starts with a consonant.
let conStart (str: string) =
match str[0] with
| 'a' | 'e' | 'i' | 'o' | 'u' -> ()
| _ -> printfn $"{str}"
// Display a string if it starts with a vowel.
let vowelStart (str: string) =
match str[0] with
| 'a' | 'e' | 'i' | 'o' | 'u' -> printfn $"{str}"
| _ -> ()
// Demonstrate the use of delegates, including the Remove and
// Combine methods to create and modify delegate combinations.
[<EntryPoint>]
let main _ =
// Declare the StringContainer class and add some strings
let container = 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.
let conStart = CheckAndDisplayDelegate StringExtensions.conStart
let vowelStart = CheckAndDisplayDelegate StringExtensions.vowelStart
// Get the list of all delegates assigned to this MulticastDelegate instance.
let delegateList = conStart.GetInvocationList()
printfn $"conStart contains {delegateList.Length} delegate(s)."
let delegateList = vowelStart.GetInvocationList()
printfn $"vowelStart contains {delegateList.Length} delegate(s).\n"
// Determine whether the delegates are System.Multicast delegates.
if box conStart :? System.MulticastDelegate && box vowelStart :? System.MulticastDelegate then
printfn "conStart and vowelStart are derived from MulticastDelegate.\n"
// Execute the two delegates.
printfn "Executing the conStart delegate:"
container.DisplayAllQualified conStart
printfn "\nExecuting the vowelStart delegate:"
container.DisplayAllQualified vowelStart
printfn ""
// Create a new MulticastDelegate and call Combine to add two delegates.
let multipleDelegates =
Delegate.Combine(conStart, vowelStart) :?> CheckAndDisplayDelegate
// How many delegates does multipleDelegates contain?
let delegateList = multipleDelegates.GetInvocationList()
printfn $"\nmultipleDelegates contains {delegateList.Length} delegates.\n"
// Pass this multicast delegate to DisplayAllQualified.
printfn "Executing the multipleDelegate delegate."
container.DisplayAllQualified multipleDelegates
// Call remove and combine to change the contained delegates.
let multipleDelegates = Delegate.Remove(multipleDelegates, vowelStart) :?> CheckAndDisplayDelegate
let multipleDelegates = Delegate.Combine(multipleDelegates, conStart) :?> CheckAndDisplayDelegate
// Pass multipleDelegates to DisplayAllQualified again.
printfn "\nExecuting the multipleDelegate delegate with two conStart delegates:"
printfn $"{multipleDelegates}"
0
// 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
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
備註
MulticastDelegate 是特殊類別。 編譯器和其他工具可以衍生自這個類別,但您無法明確衍生自它。 相同的是 類別。 Delegate
除了委派型別繼承自 MulticastDelegate 的方法之外,Common Language Runtime 還提供兩個特殊方法: BeginInvoke
和 EndInvoke
。 如需這些方法的詳細資訊,請參閱 非同步呼叫同步方法。
MulticastDelegate具有連結的委派清單,稱為叫用清單,由一或多個元素組成。 叫用多播委派時,調用清單中的委派會依出現的順序同步呼叫。 如果在執行清單期間發生錯誤,則會擲回例外狀況。
建構函式
MulticastDelegate(Object, String) |
初始化 MulticastDelegate 類別的新執行個體。 |
MulticastDelegate(Type, String) |
初始化 MulticastDelegate 類別的新執行個體。 |
屬性
HasSingleTarget |
表示多點傳送委派;也就是說,委派可以在它的引動過程清單中包含一個以上的項目。 (繼承來源 Delegate) |
Method |
取得委派所表示的方法。 (繼承來源 Delegate) |
Target |
取得目前的委派在其中叫用執行個體方法的類別執行個體。 (繼承來源 Delegate) |
方法
Clone() |
建立委派的淺層複本 (Shallow Copy)。 (繼承來源 Delegate) |
CombineImpl(Delegate) | |
CombineImpl(Delegate) |
串連指定的多點傳送 (可結合的) 委派和目前多點傳送 (可結合的) 委派的引動過程清單。 (繼承來源 Delegate) |
DynamicInvoke(Object[]) |
動態叫用 (晚期繫結) 目前委派所表示的方法。 (繼承來源 Delegate) |
DynamicInvokeImpl(Object[]) |
處理完整的引動過程清單。 |
DynamicInvokeImpl(Object[]) |
動態叫用 (晚期繫結) 目前委派所表示的方法。 (繼承來源 Delegate) |
Equals(Object) |
判斷這個多點傳送委派和指定的物件是否相等。 |
GetHashCode() |
傳回這個執行個體的雜湊碼。 |
GetInvocationList() |
依照引動過程的順序,傳回這個多點傳送委派的引動過程清單。 |
GetMethodImpl() |
傳回目前 MulticastDelegate 所表示的方法。 |
GetMethodImpl() |
取得目前委派所表示的方法。 (繼承來源 Delegate) |
GetObjectData(SerializationInfo, StreamingContext) |
已淘汰.
使用所有序列化這個執行個體所需的資料,填入 SerializationInfo 物件。 |
GetObjectData(SerializationInfo, StreamingContext) |
已淘汰.
不支援。 (繼承來源 Delegate) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
RemoveImpl(Delegate) |
從這個 MulticastDelegate 的引動過程清單中,移除和指定委派相等的項目。 |
RemoveImpl(Delegate) |
從另一個委派的引動過程清單移除委派的引動過程清單。 (繼承來源 Delegate) |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |
運算子
Equality(MulticastDelegate, MulticastDelegate) |
判斷兩個 MulticastDelegate 物件是否相等。 |
Inequality(MulticastDelegate, MulticastDelegate) |
判斷兩個 MulticastDelegate 物件是否不相等。 |
擴充方法
GetMethodInfo(Delegate) |
取得表示特定委派所代表之方法的物件。 |
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應