Type.GetEvent Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets a specific event declared or inherited by the current Type.
GetEvent(String, BindingFlags) |
When overridden in a derived class, returns the EventInfo object representing the specified event, using the specified binding constraints. |
GetEvent(String) |
Returns the EventInfo object representing the specified public event. |
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
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
Parameters
- name
- String
The string containing the name of an event which is declared or inherited by the current Type.
- bindingAttr
- BindingFlags
A bitwise combination of the enumeration values that specify how the search is conducted.
-or-
Default to return null
.
Returns
The object representing the specified event that is declared or inherited by the current Type, if found; otherwise, null
.
Implements
Exceptions
name
is null
.
Examples
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);
}
}
}
open System
open System.Reflection
open System.Security
try
// Creates a bitmask based on BindingFlags.
let myBindingFlags = BindingFlags.Instance ||| BindingFlags.Public ||| BindingFlags.NonPublic
let myTypeBindingFlags = typeof<System.Windows.Forms.Button>
let myEventBindingFlags = myTypeBindingFlags.GetEvent("Click", myBindingFlags)
if myEventBindingFlags <> null then
printfn $"Looking for the Click event in the Button class with the specified BindingFlags.\n{myEventBindingFlags}"
else
printfn "The Click event is not available with the Button class."
with
| :? SecurityException as e ->
printfn "An exception occurred."
printfn $"Message :{e.Message}"
| :? ArgumentNullException as e ->
printfn "An exception occurred."
printfn $"Message :{e.Message}"
| e ->
printfn $"The following exception was raised : {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
Remarks
The following BindingFlags filter flags can be used to define which events to include in the search:
You must specify either
BindingFlags.Instance
orBindingFlags.Static
in order to get a return.Specify
BindingFlags.Public
to include public events in the search.Specify
BindingFlags.NonPublic
to include non-public events (that is, private, internal, and protected events) in the search.Specify
BindingFlags.FlattenHierarchy
to includepublic
andprotected
static members up the hierarchy;private
static members in inherited classes are not included.
The following BindingFlags modifier flags can be used to change how the search works:
BindingFlags.IgnoreCase
to ignore the case ofname
.BindingFlags.DeclaredOnly
to search only the events declared on the Type, not events that were simply inherited.
See System.Reflection.BindingFlags for more information.
An event is considered public to reflection if it has at least one method or accessor that is public. 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.
If the current Type represents a constructed generic type, this method returns the EventInfo with the type parameters replaced by the appropriate type arguments.
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.
See also
Applies to
.NET 9 и другие версии
Продукт | Версии |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
- Source:
- Type.cs
- Source:
- Type.cs
- Source:
- Type.cs
Returns the EventInfo object representing the specified public event.
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
Parameters
- name
- String
The string containing the name of an event that is declared or inherited by the current Type.
Returns
The object representing the specified public event that is declared or inherited by the current Type, if found; otherwise, null
.
Implements
Exceptions
name
is null
.
Examples
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);
}
}
}
open System
open System.Security
try
let myType = typeof<System.Windows.Forms.Button>
let myEvent = myType.GetEvent "Click"
if myEvent <> null then
printfn $"Looking for the Click event in the Button class.\n{myEvent}"
else
printfn "The Click event is not available in the Button class."
with
| :? SecurityException as e ->
printfn "An exception occurred."
printfn $"Message :{e.Message}"
| :? ArgumentNullException as e ->
printfn "An exception occurred."
printfn $"Message :{e.Message}"
| e ->
printfn $"The following exception was raised : {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
Remarks
An event is considered public to reflection if it has at least one method or accessor that is public. 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.
The search for name
is case-sensitive. The search includes public static and public instance events.
The following table shows what members of a base class are returned by the Get
methods when reflecting on a type.
Member Type | Static | Non-Static |
---|---|---|
Constructor | No | No |
Field | No | Yes. A field is always hide-by-name-and-signature. |
Event | Not applicable | The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below. |
Method | No | Yes. A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature. |
Nested Type | No | No |
Property | Not applicable | The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below. |
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. This is a binary comparison.
For reflection, properties and events are hide-by-name-and-signature. 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.
Custom attributes are not part of the common type system.
If the current Type represents a constructed generic type, this method returns the EventInfo with the type parameters replaced by the appropriate type arguments.
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.
See also
Applies to
.NET 9 и другие версии
Продукт | Версии |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
Отзыв о .NET
.NET — это проект с открытым исходным кодом. Выберите ссылку, чтобы оставить отзыв: