Type.GetEvent Metodo
Definizione
Overload
GetEvent(String) |
Restituisce l'oggetto EventInfo che rappresenta l'evento pubblico specificato.Returns the EventInfo object representing the specified public event. |
GetEvent(String, BindingFlags) |
Quando ne viene eseguito l'override in una classe derivata, restituisce l'oggetto EventInfo che rappresenta l'evento specificato, usando i vincoli di associazione specificati.When overridden in a derived class, returns the EventInfo object representing the specified event, using the specified binding constraints. |
GetEvent(String)
public:
System::Reflection::EventInfo ^ GetEvent(System::String ^ name);
public:
virtual System::Reflection::EventInfo ^ GetEvent(System::String ^ name);
public System.Reflection.EventInfo? GetEvent (string name);
public System.Reflection.EventInfo GetEvent (string name);
member this.GetEvent : string -> System.Reflection.EventInfo
abstract member GetEvent : string -> System.Reflection.EventInfo
override this.GetEvent : string -> System.Reflection.EventInfo
Public Function GetEvent (name As String) As EventInfo
Parametri
- name
- String
Stringa contenente il nome di un evento dichiarato o ereditato dall'oggetto Type corrente.The string containing the name of an event that is declared or inherited by the current Type.
Restituisce
Oggetto che rappresenta l'evento pubblico specificato dichiarato o ereditato dall'oggetto Type corrente, se disponibile; in caso contrario, null
.The object representing the specified public event that is declared or inherited by the current Type, if found; otherwise, null
.
Implementazioni
Eccezioni
name
è null
.name
is null
.
Esempio
Nell'esempio seguente viene creato un EventInfo oggetto e viene ottenuto l'evento per una classe Button per l'evento specificato.The following example creates an EventInfo object and gets the event for a button class for the specified event.
#using <system.dll>
#using <system.windows.forms.dll>
#using <system.drawing.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Security;
int main()
{
try
{
Type^ myType = System::Windows::Forms::Button::typeid;
EventInfo^ myEvent = myType->GetEvent( "Click" );
if ( myEvent != nullptr )
{
Console::WriteLine( "Looking for the Click event in the Button class." );
Console::WriteLine( myEvent );
}
else
Console::WriteLine( "The Click event is not available in the Button class." );
}
catch ( SecurityException^ e )
{
Console::WriteLine( "An exception occurred." );
Console::WriteLine( "Message : {0}", e->Message );
}
catch ( ArgumentNullException^ e )
{
Console::WriteLine( "An exception occurred." );
Console::WriteLine( "Message : {0}", e->Message );
}
catch ( Exception^ e )
{
Console::WriteLine( "The following exception was raised : {0}", e->Message );
}
}
using System;
using System.Reflection;
using System.Security;
class MyEventExample
{
public static void Main()
{
try
{
Type myType = typeof(System.Windows.Forms.Button);
EventInfo myEvent = myType.GetEvent("Click");
if(myEvent != null)
{
Console.WriteLine("Looking for the Click event in the Button class.");
Console.WriteLine(myEvent.ToString());
}
else
{
Console.WriteLine("The Click event is not available in the Button class.");
}
}
catch(SecurityException e)
{
Console.WriteLine("An exception occurred.");
Console.WriteLine("Message :"+e.Message);
}
catch(ArgumentNullException e)
{
Console.WriteLine("An exception occurred.");
Console.WriteLine("Message :"+e.Message);
}
catch(Exception e)
{
Console.WriteLine("The following exception was raised : {0}",e.Message);
}
}
}
Imports System.Reflection
Imports System.Security
' Compile this sample using the following command line:
' vbc type_getevent.vb /r:"System.Windows.Forms.dll" /r:"System.dll"
Class MyEventExample
Public Shared Sub Main()
Try
Dim myType As Type = GetType(System.Windows.Forms.Button)
Dim myEvent As EventInfo = myType.GetEvent("Click")
If Not (myEvent Is Nothing) Then
Console.WriteLine(ControlChars.Cr + "Looking for the Click event in the Button class.")
Console.WriteLine(ControlChars.Cr + myEvent.ToString())
Else
Console.WriteLine("The Click event is not available with the Button class.")
End If
Catch e As SecurityException
Console.WriteLine("An exception occurred.")
Console.WriteLine("Message :" + e.Message)
Catch e As ArgumentNullException
Console.WriteLine("An exception occurred.")
Console.WriteLine("Message :" + e.Message)
Catch e As Exception
Console.WriteLine("The following exception was raised : {0}", e.Message)
End Try
End Sub
End Class
Commenti
Un evento viene considerato pubblico per la reflection se dispone almeno di un metodo o di una funzione di accesso pubblica.An event is considered public to reflection if it has at least one method or accessor that is public. In caso contrario, l'evento viene considerato privato ed è necessario utilizzare BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic combinare i valori utilizzando Or
) per ottenerlo.Otherwise the event is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or
) to get it.
La ricerca di name
fa distinzione tra maiuscole e minuscole.The search for name
is case-sensitive. La ricerca include eventi pubblici statici e di istanza pubblica.The search includes public static and public instance events.
La tabella seguente mostra quali membri di una classe di base vengono restituiti dai Get
metodi durante la reflection su un tipo.The following table shows what members of a base class are returned by the Get
methods when reflecting on a type.
Tipo di membroMember Type | StaticStatic | Non staticoNon-Static |
---|---|---|
CostruttoreConstructor | NoNo | NoNo |
CampoField | NoNo | Sì.Yes. Un campo è sempre nascosto per nome e firma.A field is always hide-by-name-and-signature. |
EventoEvent | Non applicabileNot applicable | La regola Common Type System è che l'ereditarietà è identica a quella dei metodi che implementano la proprietà.The common type system rule is that the inheritance is the same as that of the methods that implement the property. La reflection considera le proprietà come nascoste per nome e firma.Reflection treats properties as hide-by-name-and-signature. Vedere la nota 2 di seguito.See note 2 below. |
MetodoMethod | NoNo | Sì.Yes. Un metodo (sia virtuale che non virtuale) può essere nascosto in base al nome o nascosto per nome e firma.A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature. |
Tipo annidatoNested Type | NoNo | NoNo |
ProprietàProperty | Non applicabileNot applicable | La regola Common Type System è che l'ereditarietà è identica a quella dei metodi che implementano la proprietà.The common type system rule is that the inheritance is the same as that of the methods that implement the property. La reflection considera le proprietà come nascoste per nome e firma.Reflection treats properties as hide-by-name-and-signature. Vedere la nota 2 di seguito.See note 2 below. |
Hide-by-Name-and-signature prende in considerazione tutte le parti della firma, inclusi i modificatori personalizzati, i tipi restituiti, i tipi di parametro, le sentinelle e le convenzioni di chiamata non gestite.Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. Si tratta di un confronto binario.This is a binary comparison.
Per la reflection, le proprietà e gli eventi sono nascosti per nome e firma.For reflection, properties and events are hide-by-name-and-signature. Se si dispone di una proprietà con una funzione di accesso get e set nella classe di base, ma la classe derivata dispone solo di una funzione di accesso get, la proprietà della classe derivata nasconde la proprietà della classe base e non sarà possibile accedere al setter sulla classe di base.If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.
Gli attributi personalizzati non fanno parte del Common Type System.Custom attributes are not part of the common type system.
Se l'oggetto corrente Type rappresenta un tipo generico costruito, questo metodo restituisce l'oggetto EventInfo con i parametri di tipo sostituiti dagli argomenti di tipo appropriati.If the current Type represents a constructed generic type, this method returns the EventInfo with the type parameters replaced by the appropriate type arguments.
Se l'oggetto corrente Type rappresenta un parametro di tipo nella definizione di un tipo o di un metodo generico, questo metodo cerca gli eventi del vincolo di classe.If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the events of the class constraint.
Vedi anche
Si applica a
GetEvent(String, BindingFlags)
Quando ne viene eseguito l'override in una classe derivata, restituisce l'oggetto EventInfo che rappresenta l'evento specificato, usando i vincoli di associazione specificati.When overridden in a derived class, returns the EventInfo object representing the specified event, using the specified binding constraints.
public:
abstract System::Reflection::EventInfo ^ GetEvent(System::String ^ name, System::Reflection::BindingFlags bindingAttr);
public abstract System.Reflection.EventInfo? GetEvent (string name, System.Reflection.BindingFlags bindingAttr);
public abstract System.Reflection.EventInfo GetEvent (string name, System.Reflection.BindingFlags bindingAttr);
abstract member GetEvent : string * System.Reflection.BindingFlags -> System.Reflection.EventInfo
Public MustOverride Function GetEvent (name As String, bindingAttr As BindingFlags) As EventInfo
Parametri
- name
- String
Stringa contenente il nome di un evento dichiarato o ereditato dall'oggetto Type corrente.The string containing the name of an event which is declared or inherited by the current Type.
- bindingAttr
- BindingFlags
Combinazione bit per bit di valori di enumerazione che specifica il modo in cui viene eseguita la ricerca.A bitwise combination of the enumeration values that specify how the search is conducted.
-oppure--or-
Default per restituire null
.Default to return null
.
Restituisce
Oggetto che rappresenta l'evento specificato dichiarato o ereditato dall'oggetto Type corrente, se disponibile; in caso contrario, null
.The object representing the specified event that is declared or inherited by the current Type, if found; otherwise, null
.
Implementazioni
Eccezioni
name
è null
.name
is null
.
Esempio
Nell'esempio di codice seguente viene usato il GetEvent(String, BindingFlags) metodo per eseguire una ricerca in un tipo per un evento pubblico o non pubblico denominato "click" che non è static
( Shared
in Visual Basic).The following code example uses the GetEvent(String, BindingFlags) method to search a type for a public or non-public event named "Click" that is not static
(Shared
in Visual Basic).
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
using namespace System;
using namespace System::Reflection;
using namespace System::Security;
using namespace System::Windows::Forms;
int main()
{
try
{
// Creates a bitmask based on BindingFlags.
BindingFlags myBindingFlags = static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public | BindingFlags::NonPublic);
Type^ myTypeBindingFlags = System::Windows::Forms::Button::typeid;
EventInfo^ myEventBindingFlags = myTypeBindingFlags->GetEvent( "Click", myBindingFlags );
if ( myEventBindingFlags != nullptr )
{
Console::WriteLine( "Looking for the Click event in the Button class with the specified BindingFlags." );
Console::WriteLine( myEventBindingFlags );
}
else
Console::WriteLine( "The Click event is not available with the Button class." );
}
catch ( SecurityException^ e )
{
Console::WriteLine( "An exception occurred." );
Console::WriteLine( "Message : {0}", e->Message );
}
catch ( ArgumentNullException^ e )
{
Console::WriteLine( "An exception occurred." );
Console::WriteLine( "Message : {0}", e->Message );
}
catch ( Exception^ e )
{
Console::WriteLine( "The following exception was raised : {0}", e->Message );
}
}
using System;
using System.Reflection;
using System.Security;
class MyEventExample
{
public static void Main()
{
try
{
// Creates a bitmask based on BindingFlags.
BindingFlags myBindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
Type myTypeBindingFlags = typeof(System.Windows.Forms.Button);
EventInfo myEventBindingFlags = myTypeBindingFlags.GetEvent("Click", myBindingFlags);
if(myEventBindingFlags != null)
{
Console.WriteLine("Looking for the Click event in the Button class with the specified BindingFlags.");
Console.WriteLine(myEventBindingFlags.ToString());
}
else
{
Console.WriteLine("The Click event is not available with the Button class.");
}
}
catch(SecurityException e)
{
Console.WriteLine("An exception occurred.");
Console.WriteLine("Message :"+e.Message);
}
catch(ArgumentNullException e)
{
Console.WriteLine("An exception occurred.");
Console.WriteLine("Message :"+e.Message);
}
catch(Exception e)
{
Console.WriteLine("The following exception was raised : {0}",e.Message);
}
}
}
Imports System.Reflection
Imports System.Security
' Compile this sample using the following command line:
' vbc type_getevent.vb /r:"System.Windows.Forms.dll" /r:"System.dll"
Class MyEventExample
Public Shared Sub Main()
Try
' Creates a bitmask comprising BindingFlags.
Dim myBindingFlags As BindingFlags = BindingFlags.Instance Or BindingFlags.Public _
Or BindingFlags.NonPublic
Dim myTypeBindingFlags As Type = GetType(System.Windows.Forms.Button)
Dim myEventBindingFlags As EventInfo = myTypeBindingFlags.GetEvent("Click", myBindingFlags)
If myEventBindingFlags IsNot Nothing Then
Console.WriteLine("Looking for the Click event in the Button class with the specified BindingFlags.")
Console.WriteLine(myEventBindingFlags.ToString())
Else
Console.WriteLine("The Click event is not available with the Button class.")
End If
Catch e As SecurityException
Console.WriteLine("An exception occurred.")
Console.WriteLine("Message :" + e.Message)
Catch e As ArgumentNullException
Console.WriteLine("An exception occurred.")
Console.WriteLine("Message :" + e.Message)
Catch e As Exception
Console.WriteLine("The following exception was raised : {0}", e.Message)
End Try
End Sub
End Class
Commenti
I BindingFlags flag di filtro seguenti possono essere usati per definire gli eventi da includere nella ricerca:The following BindingFlags filter flags can be used to define which events to include in the search:
È necessario specificare
BindingFlags.Instance
o perBindingFlags.Static
ottenere un valore restituito.You must specify eitherBindingFlags.Instance
orBindingFlags.Static
in order to get a return.Specificare
BindingFlags.Public
per includere gli eventi pubblici nella ricerca.SpecifyBindingFlags.Public
to include public events in the search.Specificare
BindingFlags.NonPublic
per includere eventi non pubblici (ovvero eventi privati, interni e protetti) nella ricerca.SpecifyBindingFlags.NonPublic
to include non-public events (that is, private, internal, and protected events) in the search.BindingFlags.FlattenHierarchy
Consente di specificare di includerepublic
eprotected
membri statici nella gerarchiaprivate
. i membri statici nelle classi ereditate non sono inclusi.SpecifyBindingFlags.FlattenHierarchy
to includepublic
andprotected
static members up the hierarchy;private
static members in inherited classes are not included.
I BindingFlags flag di modifica seguenti possono essere utilizzati per modificare il funzionamento della ricerca:The following BindingFlags modifier flags can be used to change how the search works:
BindingFlags.IgnoreCase
per ignorare la distinzione tra maiuscole e minuscolename
.BindingFlags.IgnoreCase
to ignore the case ofname
.BindingFlags.DeclaredOnly
per cercare solo gli eventi dichiarati in Type , non gli eventi che sono stati semplicemente ereditati.BindingFlags.DeclaredOnly
to search only the events declared on the Type, not events that were simply inherited.
Per altre informazioni, vedere System.Reflection.BindingFlags.See System.Reflection.BindingFlags for more information.
Un evento viene considerato pubblico per la reflection se dispone almeno di un metodo o di una funzione di accesso pubblica.An event is considered public to reflection if it has at least one method or accessor that is public. In caso contrario, l'evento viene considerato privato ed è necessario utilizzare BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic combinare i valori utilizzando Or
) per ottenerlo.Otherwise the event is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or
) to get it.
Se l'oggetto corrente Type rappresenta un tipo generico costruito, questo metodo restituisce l'oggetto EventInfo con i parametri di tipo sostituiti dagli argomenti di tipo appropriati.If the current Type represents a constructed generic type, this method returns the EventInfo with the type parameters replaced by the appropriate type arguments.
Se l'oggetto corrente Type rappresenta un parametro di tipo nella definizione di un tipo o di un metodo generico, questo metodo cerca gli eventi del vincolo di classe.If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the events of the class constraint.