Type.GetEvent Yöntem

Tanım

Geçerli Typetarafından bildirilen veya devralınan belirli bir olayı alır.

Aşırı Yüklemeler

Name Description
GetEvent(String, BindingFlags)

Türetilmiş bir sınıfta geçersiz kılındığında, belirtilen bağlama kısıtlamalarını kullanarak belirtilen olayı temsil eden nesneyi döndürür EventInfo .

GetEvent(String)

EventInfo Belirtilen ortak olayı temsil eden nesneyi döndürür.

GetEvent(String, BindingFlags)

Kaynak:
Type.cs
Kaynak:
Type.cs
Kaynak:
Type.cs
Kaynak:
Type.cs
Kaynak:
Type.cs

Türetilmiş bir sınıfta geçersiz kılındığında, belirtilen bağlama kısıtlamalarını kullanarak belirtilen olayı temsil eden nesneyi döndürür EventInfo .

public:
 abstract System::Reflection::EventInfo ^ GetEvent(System::String ^ name, System::Reflection::BindingFlags bindingAttr);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicEvents | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicEvents)]
public abstract System.Reflection.EventInfo? GetEvent(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);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicEvents | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicEvents)>]
abstract member GetEvent : string * System.Reflection.BindingFlags -> System.Reflection.EventInfo
abstract member GetEvent : string * System.Reflection.BindingFlags -> System.Reflection.EventInfo
Public MustOverride Function GetEvent (name As String, bindingAttr As BindingFlags) As EventInfo

Parametreler

name
String

Geçerli Typetarafından bildirilen veya devralınan bir olayın adı.

bindingAttr
BindingFlags

Aramanın nasıl yapıldığını belirten numaralandırma değerlerinin bit düzeyinde birleşimi.

-veya-

Default öğesini döndürecek null.

Döndürülenler

Geçerli Typetarafından bildirilen veya devralınan belirtilen olayı temsil eden nesne bulunursa; aksi takdirde, null.

Uygulamalar

Öznitelikler

Özel durumlar

name, null'e eşittir.

Örnekler

Aşağıdaki kod örneği, GetEvent(String, BindingFlags) yöntemini kullanarak static (Visual Basic'de Shared) olmayan "Click" adlı genel veya genel olmayan bir olayı arar.

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

Açıklamalar

Aşağıdaki BindingFlags filtre bayrakları, aramaya hangi olayların dahil edilebileceğini tanımlamak için kullanılabilir:

  • İade almak için ya BindingFlags.Instance ya da BindingFlags.Static belirtmelisiniz.

  • Genel olayları aramaya dahil etmek için belirtin BindingFlags.Public .

  • Genel olmayan olayları (özel, iç ve korumalı olaylar) aramaya dahil etmek için belirtin BindingFlags.NonPublic .

  • BelirtinBindingFlags.FlattenHierarchy: public ve protected statik üyeleri hiyerarşiye eklemek için; türetilmiş sınıflardaki private statik üyeler dahil edilmez.

Aramanın çalışma biçimini değiştirmek için aşağıdaki BindingFlags değiştirici bayraklar kullanılabilir:

  • BindingFlags.IgnoreCase öğesinin büyük/küçük harf durumunu nameyoksaymak için.

  • BindingFlags.DeclaredOnly yalnızca üzerinde Typebildirilen olayları aramak için yalnızca devralınan olayları aramaz.

Daha fazla bilgi için bkz. System.Reflection.BindingFlags.

Bir olay, ortak olan en az bir yönteme veya erişimciye sahipse yansımaya genel olarak kabul edilir. Aksi takdirde olay özel olarak kabul edilir ve almak için BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static kullanmanız gerekir (Visual Basic Or kullanarak değerleri birleştirin).

Geçerli Type yapılandırılmış bir genel türü temsil ediyorsa, bu yöntem tür parametrelerini uygun tür bağımsız değişkenleriyle değiştirerek EventInfo'i döndürür.

Geçerli Type , genel bir tür veya genel yöntemin tanımında tür parametresini temsil ederse, bu yöntem sınıf kısıtlamasının olaylarını arar.

Ayrıca bkz.

Şunlara uygulanır

GetEvent(String)

Kaynak:
Type.cs
Kaynak:
Type.cs
Kaynak:
Type.cs
Kaynak:
Type.cs
Kaynak:
Type.cs

EventInfo Belirtilen ortak olayı temsil eden nesneyi döndürür.

public:
 System::Reflection::EventInfo ^ GetEvent(System::String ^ name);
public:
 virtual System::Reflection::EventInfo ^ GetEvent(System::String ^ name);
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicEvents)]
public System.Reflection.EventInfo? GetEvent(string name);
public System.Reflection.EventInfo GetEvent(string name);
public System.Reflection.EventInfo? GetEvent(string name);
[<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicEvents)>]
member this.GetEvent : string -> System.Reflection.EventInfo
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

Parametreler

name
String

Geçerli Typetarafından bildirilen veya devralınan bir olayın adını içeren dize.

Döndürülenler

Geçerli Typetarafından bildirilen veya devralınan belirtilen ortak olayı temsil eden nesne, bulunursa; değilse, null.

Uygulamalar

Öznitelikler

Özel durumlar

name, null'e eşittir.

Örnekler

Aşağıdaki örnek bir EventInfo nesne oluşturur ve belirtilen olay için bir düğme sınıfı için olayı alır.

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

Açıklamalar

Bir olay, ortak olan en az bir yönteme veya erişimciye sahipse yansımaya genel olarak kabul edilir. Aksi takdirde olay özel olarak kabul edilir ve almak için BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static kullanmanız gerekir (Visual Basic Or kullanarak değerleri birleştirin).

name araması büyük/küçük harfe duyarlıdır. Arama, genel statik ve genel örnek olaylarını içerir.

Aşağıdaki tabloda, bir türe yansıtılırken yöntemler tarafından döndürülen temel sınıf üyeleri gösterilmektedir.

Üye Türü Static Statik Olmayan
Yapıcı No No
Alan No Yes. Alan her zaman isim ve imzaya göre gizli olur.
Olay Uygulanamaz Yaygın tür sistemi kuralı, özniteliği uygulayan yöntemlerdeki devralma ile aynıdır. Yansıma, özellikleri ada ve imzaya göre gizle olarak ele alır. Aşağıdaki 2. nota bakın.
Yöntem No Yes. Bir yöntem (hem sanal hem de sanal olmayan) isim odaklı gizleme veya isim ve imza odaklı gizleme olabilir.
İç İçe Yerleştirilmiş Tür No No
Mülkiyet Uygulanamaz Yaygın tür sistemi kuralı, özniteliği uygulayan yöntemlerdeki devralma ile aynıdır. Yansıma, özellikleri ada ve imzaya göre gizle olarak ele alır. Aşağıdaki 2. nota bakın.
  1. Ad ve imzaya göre gizleme, özel değiştiriciler, dönüş türleri, parametre türleri, sentineller ve yönetilmeyen çağırma kuralları dahil olmak üzere, imzanın tüm parçalarını dikkate alır. Bu ikili karşılaştırmadır.

  2. Yansıma için, özellikler ve olaylar ada ve imzaya göre gizlenir. Temel sınıfta hem get hem de set erişimcisine sahip bir özelliğiniz varsa, ancak türetilen sınıfın yalnızca bir get erişimcisi varsa, türetilmiş sınıf özelliği temel sınıf özelliğini gizler ve temel sınıfta ayarlayıcıya erişemezsiniz.

  3. Özel öznitelikler ortak tür sisteminin bir parçası değildir.

Geçerli Type yapılandırılmış bir genel türü temsil ediyorsa, bu yöntem tür parametrelerini uygun tür bağımsız değişkenleriyle değiştirerek EventInfo'i döndürür.

Geçerli Type , genel bir tür veya genel yöntemin tanımında tür parametresini temsil ederse, bu yöntem sınıf kısıtlamasının olaylarını arar.

Ayrıca bkz.

Şunlara uygulanır