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
표시하는 데 사용되는 대리자입니다. 대리자는 단일 문자열을 매개 변수로 사용하고 를 반환 void
합니다(또는 Visual Basic에서는 프로시저임 Sub
). 또한 단일 매개 변수인 DisplayAllQualified
대리자를 포함하는 메서드도 CheckAndDisplayDelegate
포함됩니다. 이렇게 하면 메서드를 호출하고 대리자가 포함하는 메서드에 따라 필터링된 문자열 집합을 표시할 수 있습니다.
이 예제에서는 다음 두 가지 메서드가 있는 유틸리티 클래스 StringExtensions
를 정의합니다.
ConStart
- 자음으로 시작하는 문자열을 표시합니다.VowelStart
모음으로 시작하는 문자열을 표시합니다.
두 메서드 모두 단일 문자열 매개 변수를 포함하고 를 반환 void
합니다. 즉, 두 메서드를 모두 대리자 CheckAndDisplayDelegate
에게 할당할 수 있습니다.
Test.Main
메서드는 애플리케이션 진입점입니다. 개체를 StringContainer
인스턴스화하고, 문자열로 채우고, 단일 메서드를 호출하는 두 개의 CheckAndDisplayDelegate
대리자 conStart
및 vowelStart
를 만듭니다. 그런 다음 메서드를 Delegate.Combine 호출하여 처음에 및 VowelStart
대리자를 포함하는 대리자를 ConStart
만듭니 multipleDelegates
다. 대리자를 호출하면 컬렉션의 multipleDelegates
모든 문자열이 원래 순서로 표시됩니다. 각 문자가 각 대리자로 개별적으로 전달되고 각 문자가 두 대리자 중 하나의 필터링 조건을 충족하기 때문입니다. 마지막으로 및 Delegate.CombinemultipleDelegates
를 호출한 Delegate.Remove 후 두 개의 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하는 메서드 외에도 공용 언어 런타임은 및 EndInvoke
의 두 가지 BeginInvoke
특수 메서드를 제공합니다. 이러한 메서드에 대한 자세한 내용은 동기 메서드 를 비동기적으로 호출을 참조하세요.
MulticastDelegate 에는 하나 이상의 요소로 구성된 호출 목록이라고 하는 대리자의 연결된 목록이 있습니다. 멀티캐스트 대리자를 호출하면 호출 목록의 대리자는 표시되는 순서대로 동기적으로 호출됩니다. 목록을 실행하는 동안 오류가 발생하면 예외가 throw됩니다.
생성자
MulticastDelegate(Object, String) |
MulticastDelegate 클래스의 새 인스턴스를 초기화합니다. |
MulticastDelegate(Type, String) |
MulticastDelegate 클래스의 새 인스턴스를 초기화합니다. |
속성
Method |
대리자가 나타내는 메서드를 가져옵니다. (다음에서 상속됨 Delegate) |
Target |
현재 대리자가 인스턴스 메서드를 호출하는 클래스 인스턴스를 가져옵니다. (다음에서 상속됨 Delegate) |
메서드
Clone() |
대리자의 부분 복사본을 만듭니다. (다음에서 상속됨 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) |
사용되지 않습니다.
이 인스턴스를 serialize하는 데 필요한 모든 데이터로 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) |
지정된 대리자가 나타내는 메서드를 나타내는 개체를 가져옵니다. |