EventInfo 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
探索事件的屬性並提供事件中繼資料的存取。
public ref class EventInfo abstract : System::Reflection::MemberInfo
public ref class EventInfo abstract : System::Reflection::MemberInfo, System::Runtime::InteropServices::_EventInfo
public abstract class EventInfo : System.Reflection.MemberInfo
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public abstract class EventInfo : System.Reflection.MemberInfo, System.Runtime.InteropServices._EventInfo
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public abstract class EventInfo : System.Reflection.MemberInfo, System.Runtime.InteropServices._EventInfo
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public abstract class EventInfo : System.Reflection.MemberInfo
type EventInfo = class
inherit MemberInfo
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
type EventInfo = class
inherit MemberInfo
interface _EventInfo
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Serializable>]
type EventInfo = class
inherit MemberInfo
interface _EventInfo
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Serializable>]
type EventInfo = class
inherit MemberInfo
Public MustInherit Class EventInfo
Inherits MemberInfo
Public MustInherit Class EventInfo
Inherits MemberInfo
Implements _EventInfo
- 繼承
- 衍生
- 屬性
- 實作
範例
下列程式碼會取得 EventInfoClick 類別事件的物件 Button 。
#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
備註
使用 類別 EventInfo 來檢查事件和連結事件處理常式,如 方法的 AddEventHandler 範例程式碼所示。
注意
EventInfo 不是用來引發事件。 物件會依照其內部狀態來引發事件。
事件會與委派搭配使用。 事件接聽程式會具現化每當事件來源引發事件時叫用的事件處理常式委派。 為了連接到事件來源,事件接聽程式會將此委派新增至來源上的調用清單。 引發事件時,會呼叫事件處理常式委派的叫用方法。 支援多播和單一轉換事件通知。 Add
和 Remove
方法以及與事件相關聯的事件處理常式委派類別必須在中繼資料中標示。
委派是物件導向函式指標。 在 C 或 C++ 中,函式指標是方法的參考。 相較于 C 或 C++ 函式指標,委派包含兩個參考:方法的參考,以及支援 方法的物件參考。 委派可以叫用方法,而不知道宣告或繼承方法的類別類型。 委派只需要知道方法的傳回類型和參數清單。
事件模型同樣適用于單一轉換和多播委派。 呼叫委派的叫用方法時,只有單一物件會有一個呼叫的方法。 多播修飾詞可以套用至委派宣告,允許呼叫委派的叫用方法時呼叫多個方法。
EventInfo
當 的 inherit
true
GetCustomAttributes
參數未逐步執行型別階層時,呼叫 。 ICustomAttributeProvider.GetCustomAttributes 使用 System.Attribute 來繼承自訂屬性。
給實施者的注意事項
當您繼承自 EventInfo
時,必須覆寫下列成員: GetAddMethod(Boolean) 、 GetRemoveMethod(Boolean) 和 GetRaiseMethod(Boolean) 。
建構函式
EventInfo() |
初始化 |
屬性
AddMethod |
取得事件之 MethodInfo 方法 (包括非公用方法) 的 AddEventHandler(Object, Delegate) 物件。 |
Attributes |
取得這個事件的屬性。 |
CustomAttributes |
取得包含此成員之自訂屬性的集合。 (繼承來源 MemberInfo) |
DeclaringType |
取得宣告這個成員的類別。 (繼承來源 MemberInfo) |
EventHandlerType |
取得與這個事件相關之目前的事件處理常式委派之 |
IsCollectible |
取得指出此 MemberInfo 物件是否為可回收 AssemblyLoadContext 中保存之組件一部分的值。 (繼承來源 MemberInfo) |
IsMulticast |
取得值,指出事件是否為多點傳送。 |
IsSpecialName |
取得值,指出 |
MemberType |
取得 MemberTypes 值,表示這個成員為事件。 |
MemberType |
在衍生類別中覆寫時,取得 MemberTypes 值,指出成員類型︰方法、建構函式、事件等。 (繼承來源 MemberInfo) |
MetadataToken |
取得值,這個值可識別中繼資料項目。 (繼承來源 MemberInfo) |
Module |
取得用於定義型別的模組,該型別宣告以目前 MemberInfo 表示的成員。 (繼承來源 MemberInfo) |
Name |
取得目前成員的名稱。 (繼承來源 MemberInfo) |
RaiseMethod |
取得引發事件時所呼叫的方法,包括非公用方法。 |
ReflectedType |
取得類別物件,是用來取得這個 |
RemoveMethod |
取得用來移除事件方法 (包括非公用方法) 的 |
方法
AddEventHandler(Object, Delegate) |
將事件處理常式加入至事件來源。 |
Equals(Object) |
傳回值,這個值指出此執行個體是否與指定的物件相等。 |
Equals(Object) |
傳回值,這個值指出此執行個體是否與指定的物件相等。 (繼承來源 MemberInfo) |
GetAddMethod() |
傳回用來將事件處理常式委派加入至事件來源的方法。 |
GetAddMethod(Boolean) |
在衍生類別中覆寫時,針對事件的 AddEventHandler(Object, Delegate) 方法擷取 |
GetCustomAttributes(Boolean) |
在衍生類別中覆寫時,傳回套用至此成員之所有自訂屬性的陣列。 (繼承來源 MemberInfo) |
GetCustomAttributes(Type, Boolean) |
當在衍生的類別中覆寫時,會傳回套用至這個成員的自訂屬性陣列,並以 Type 識別。 (繼承來源 MemberInfo) |
GetCustomAttributesData() |
傳回 CustomAttributeData 物件的清單,表示已套用至目標成員之屬性的資料。 (繼承來源 MemberInfo) |
GetHashCode() |
傳回這個執行個體的雜湊碼。 |
GetHashCode() |
傳回這個執行個體的雜湊碼。 (繼承來源 MemberInfo) |
GetOtherMethods() |
傳回使用 |
GetOtherMethods(Boolean) |
傳回使用 |
GetRaiseMethod() |
傳回引發事件時所呼叫的方法。 |
GetRaiseMethod(Boolean) |
在衍生類別中覆寫時,傳回在引發事件時所呼叫的方法,指定是否要傳回非公用方法。 |
GetRemoveMethod() |
傳回用來從事件來源移除事件處理常式委派的方法。 |
GetRemoveMethod(Boolean) |
在衍生類別中覆寫時,擷取 |
GetType() |
探索類別事件的屬性並提供事件中繼資料的存取。 |
GetType() |
探索成員的屬性並提供成員中繼資料的存取。 (繼承來源 MemberInfo) |
HasSameMetadataDefinitionAs(MemberInfo) |
探索事件的屬性並提供事件中繼資料的存取。 (繼承來源 MemberInfo) |
IsDefined(Type, Boolean) |
在衍生類別中覆寫時,表示是否已有一個或多個具有指定型別或其衍生型別的屬性套用至這個成員。 (繼承來源 MemberInfo) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
RemoveEventHandler(Object, Delegate) |
從事件來源移除事件處理常式。 |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |
運算子
Equality(EventInfo, EventInfo) |
表示兩個 EventInfo 物件是否相等。 |
Inequality(EventInfo, EventInfo) |
表示兩個 EventInfo 物件是否不相等。 |
明確介面實作
擴充方法
適用於
執行緒安全性
此型別具備執行緒安全。