MulticastDelegate Classe
TOC
Comprimi il sommario
Espandi il sommario
Questo articolo è stato tradotto automaticamente. Per visualizzare l'articolo in inglese, selezionare la casella di controllo Inglese. È possibile anche visualizzare il testo inglese in una finestra popup posizionando il puntatore del mouse sopra il testo.
Traduzione
Inglese

Classe MulticastDelegate

 

Rappresenta un delegato multicast, ossia un delegato che può avere più di un elemento nel suo elenco di chiamata.

Spazio dei nomi:   System
Assembly:  mscorlib (in mscorlib.dll)


[SerializableAttribute]
[ComVisibleAttribute(true)]
public abstract class MulticastDelegate : Delegate

NomeDescrizione
System_CAPS_protmethodMulticastDelegate(Object, String)

Inizializza una nuova istanza della classe MulticastDelegate.

System_CAPS_protmethodMulticastDelegate(Type, String)

Inizializza una nuova istanza della classe MulticastDelegate.

NomeDescrizione
System_CAPS_pubpropertyMethod

Ottiene il metodo rappresentato dal delegato.(Ereditato da Delegate.)

System_CAPS_pubpropertyTarget

Ottiene l'istanza di classe sulla quale il metodo di istanza viene richiamato dal delegato corrente.(Ereditato da Delegate.)

NomeDescrizione
System_CAPS_pubmethodClone()

Crea una copia superficiale del delegato.(Ereditato da Delegate.)

System_CAPS_protmethodCombineImpl(Delegate)

Associa l'oggetto Delegate con l'oggetto Delegate specificato per creare un nuovo delegato.(Esegue l’override di Delegate.CombineImpl(Delegate)).

System_CAPS_pubmethodDynamicInvoke(Object[])

Richiama dinamicamente, ovvero ad associazione tardiva, il metodo rappresentato dal delegato corrente.(Ereditato da Delegate.)

System_CAPS_protmethodDynamicInvokeImpl(Object[])

Richiama dinamicamente, ovvero ad associazione tardiva, il metodo rappresentato dal delegato corrente.(Ereditato da Delegate.)

System_CAPS_pubmethodEquals(Object)

Determina se questo delegato multicast e l'oggetto specificato sono uguali.(Esegue l’override di Delegate.Equals(Object)).

System_CAPS_protmethodFinalize()

Consente a un oggetto di provare a liberare risorse ed eseguire altre operazioni di pulizia prima che l'oggetto stesso venga reclamato dalla procedura di Garbage Collection. (Ereditato da Object.)

System_CAPS_pubmethodGetHashCode()

Restituisce il codice hash per questa istanza. (Esegue l’override di Delegate.GetHashCode()).

System_CAPS_pubmethodGetInvocationList()

Restituisce l'elenco chiamate di questo delegato multicast, in ordine di chiamata.(Esegue l’override di Delegate.GetInvocationList()).

System_CAPS_protmethodGetMethodImpl()

Restituisce un metodo statico rappresentato dall'oggetto MulticastDelegate corrente.(Esegue l’override di Delegate.GetMethodImpl()).

System_CAPS_pubmethodGetObjectData(SerializationInfo, StreamingContext)

Compila un oggetto SerializationInfo con tutti i dati necessari per serializzare questa istanza.(Esegue l’override di Delegate.GetObjectData(SerializationInfo, StreamingContext)).

System_CAPS_pubmethodGetType()

Ottiene il Type dell'istanza corrente. (Ereditato da Object.)

System_CAPS_protmethodMemberwiseClone()

Crea una copia dei riferimenti dell'oggetto Object corrente. (Ereditato da Object.)

System_CAPS_protmethodRemoveImpl(Delegate)

Rimuove un elemento uguale al delegato specificato dall'elenco chiamate di questo oggetto MulticastDelegate.(Esegue l’override di Delegate.RemoveImpl(Delegate)).

System_CAPS_pubmethodToString()

Restituisce una stringa che rappresenta l'oggetto corrente. (Ereditato da Object.)

NomeDescrizione
System_CAPS_puboperatorSystem_CAPS_staticEquality(MulticastDelegate, MulticastDelegate)

Determina se due oggetti MulticastDelegate sono uguali.

System_CAPS_puboperatorSystem_CAPS_staticInequality(MulticastDelegate, MulticastDelegate)

Determina se due oggetti MulticastDelegate non sono uguali.

NomeDescrizione
System_CAPS_pubmethodGetMethodInfo()

Ottiene un oggetto che rappresenta il metodo rappresentato dal delegato specificato.(Definito da RuntimeReflectionExtensions).

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.

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

Universal Windows Platform
Disponibile da 4.5
.NET Framework
Disponibile da 1.1
Portable Class Library
Supportato in: portable .NET platforms
Silverlight
Disponibile da 2.0
Windows Phone Silverlight
Disponibile da 7.0
Windows Phone
Disponibile da 8.1

Eventuali membri pubblici statici (Shared in Visual Basic) di questo tipo sono thread-safe. I membri di istanza non sono garantiti come thread-safe.

Torna all'inizio
Mostra:
© 2016 Microsoft