MulticastDelegate Kelas
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mewakili delegasi multicast; artinya, delegasi yang dapat memiliki lebih dari satu elemen dalam daftar pemanggilannya.
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
- Warisan
- Atribut
Contoh
Contoh berikut mendefinisikan kelas, StringContainer, yang mencakup kumpulan string. Salah satu anggotanya adalah CheckAndDisplayDelegate delegasi, yang digunakan untuk menampilkan string yang StringContainer disimpan dalam objek yang memenuhi kriteria tertentu. Delegasi mengambil satu string sebagai parameter dan mengembalikan void (atau, dalam Visual Basic, ini adalah prosedur Sub). Ini juga mencakup metode, DisplayAllQualified, yang memiliki satu parameter, CheckAndDisplayDelegate delegasi. Ini memungkinkan metode untuk dipanggil dan menampilkan sekumpulan string yang difilter berdasarkan metode yang dikandung delegasi.
Contoh ini juga mendefinisikan kelas utilitas, StringExtensions, yang memiliki dua metode:
ConStart, yang menampilkan string yang dimulai dengan konsonan.VowelStart, yang menampilkan string yang dimulai dengan vokal.
Perhatikan bahwa kedua metode menyertakan parameter string tunggal dan mengembalikan void. Dengan kata lain, kedua metode dapat ditetapkan ke CheckAndDisplayDelegate delegasi.
Metode ini Test.Main adalah titik masuk aplikasi. Ini membuat StringContainer instans objek, mengisinya dengan string, dan membuat dua CheckAndDisplayDelegate delegasi, conStart dan vowelStart, yang memanggil satu metode. Kemudian memanggil Delegate.Combine metode untuk membuat multipleDelegates delegasi, yang awalnya berisi ConStart delegasi dan VowelStart . Perhatikan bahwa ketika multipleDelegates delegasi dipanggil, itu menampilkan semua string dalam koleksi dalam urutan aslinya. Ini karena setiap huruf diteruskan secara terpisah ke setiap delegasi, dan setiap huruf memenuhi kriteria pemfilteran hanya satu dari dua delegasi. Terakhir, setelah panggilan ke Delegate.Remove dan Delegate.Combine, multipleDelegates berisi dua conStart delegasi. Saat dipanggil, setiap string dalam StringContainer objek ditampilkan dua kali.
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
Keterangan
MulticastDelegate adalah kelas khusus. Pengkompilasi dan alat lain dapat berasal dari kelas ini, tetapi Anda tidak dapat memperolehnya secara eksplisit. Hal yang sama berlaku untuk Delegate kelas.
Selain metode yang mendelegasikan jenis yang diwarisi dari MulticastDelegate, runtime bahasa umum menyediakan dua metode khusus: BeginInvoke dan EndInvoke. Untuk informasi selengkapnya tentang metode ini, lihat Memanggil Metode Sinkron Secara Asinkron.
MulticastDelegate memiliki daftar delegasi yang ditautkan, yang disebut daftar pemanggilan, yang terdiri dari satu atau beberapa elemen. Ketika delegasi multicast dipanggil, delegasi dalam daftar pemanggilan dipanggil secara sinkron dalam urutan munculnya. Jika terjadi kesalahan selama eksekusi daftar, pengecualian akan dilemparkan.
Konstruktor
| Nama | Deskripsi |
|---|---|
| MulticastDelegate(Object, String) |
Menginisialisasi instans baru dari kelas MulticastDelegate. |
| MulticastDelegate(Type, String) |
Menginisialisasi instans baru dari kelas MulticastDelegate. |
Properti
| Nama | Deskripsi |
|---|---|
| HasSingleTarget |
Mendapatkan nilai yang menunjukkan apakah Delegate memiliki satu target pemanggilan. (Diperoleh dari Delegate) |
| Method |
Mendapatkan metode yang diwakili oleh delegasi. (Diperoleh dari Delegate) |
| Target |
Mendapatkan instans kelas tempat delegasi saat ini memanggil metode instans. (Diperoleh dari Delegate) |
Metode
| Nama | Deskripsi |
|---|---|
| Clone() |
Membuat salinan delegasi yang dangkal. (Diperoleh dari Delegate) |
| CombineImpl(Delegate) |
Menggabungkan ini Delegate dengan yang ditentukan Delegate untuk membentuk delegasi baru. |
| DynamicInvoke(Object[]) |
Memanggil secara dinamis (terlambat terikat) metode yang diwakili oleh delegasi saat ini. (Diperoleh dari Delegate) |
| DynamicInvokeImpl(Object[]) |
Memproses daftar pemanggilan lengkap. |
| DynamicInvokeImpl(Object[]) |
Memanggil secara dinamis (terlambat terikat) metode yang diwakili oleh delegasi saat ini. (Diperoleh dari Delegate) |
| Equals(Object) |
Menentukan apakah delegasi multicast ini dan objek yang ditentukan sama. |
| GetHashCode() |
Mengembalikan kode hash untuk instans ini. |
| GetInvocationList() |
Mengembalikan daftar pemanggilan delegasi multicast ini, dalam urutan pemanggilan. |
| GetMethodImpl() |
Mengembalikan metode yang diwakili oleh .MulticastDelegate |
| GetMethodImpl() |
Mendapatkan metode yang diwakili oleh delegasi saat ini. (Diperoleh dari Delegate) |
| GetObjectData(SerializationInfo, StreamingContext) |
Kedaluwarsa.
Mengisi SerializationInfo objek dengan semua data yang diperlukan untuk membuat serial instans ini. |
| GetType() |
Mendapatkan Type instans saat ini. (Diperoleh dari Object) |
| MemberwiseClone() |
Membuat salinan dangkal dari Objectsaat ini. (Diperoleh dari Object) |
| RemoveImpl(Delegate) |
Menghapus elemen dari daftar pemanggilan ini MulticastDelegate yang sama dengan delegasi yang ditentukan. |
| ToString() |
Mengembalikan string yang mewakili objek saat ini. (Diperoleh dari Object) |
Operator
| Nama | Deskripsi |
|---|---|
| Equality(MulticastDelegate, MulticastDelegate) |
Menentukan apakah dua objek MulticastDelegate sama. |
| Inequality(MulticastDelegate, MulticastDelegate) |
Menentukan apakah dua MulticastDelegate objek tidak sama. |
Metode Ekstensi
| Nama | Deskripsi |
|---|---|
| GetMethodInfo(Delegate) |
Mendapatkan objek yang mewakili metode yang diwakili oleh delegasi yang ditentukan. |