DefaultEventAttribute 類別

定義

指定元件的預設事件。

public ref class DefaultEventAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class)]
public sealed class DefaultEventAttribute : Attribute
public sealed class DefaultEventAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class)>]
type DefaultEventAttribute = class
    inherit Attribute
type DefaultEventAttribute = class
    inherit Attribute
Public NotInheritable Class DefaultEventAttribute
Inherits Attribute
繼承
DefaultEventAttribute
屬性

範例

下列範例會定義名為的 MyCollection集合類別。 類別會以 DefaultEventAttribute 指定 CollectionChanged 為預設事件的標記。

[DefaultEvent("CollectionChanged")]
public ref class TestCollection: public BaseCollection
{
private:
    CollectionChangeEventHandler^ onCollectionChanged;
    
public:
    event CollectionChangeEventHandler^ CollectionChanged 
    {
    public:
        void add(CollectionChangeEventHandler^ eventHandler)
        { 
            onCollectionChanged += eventHandler; 
        }

    protected:
        void remove(CollectionChangeEventHandler^ eventHandler) 
        { 
            onCollectionChanged -= eventHandler; 
        }
    }
    // Insert additional code.
};
[DefaultEvent("CollectionChanged")]
public class MyCollection : BaseCollection {
     
    private CollectionChangeEventHandler onCollectionChanged;
     
    public event CollectionChangeEventHandler CollectionChanged {
       add {
          onCollectionChanged += value;
       }
       remove {
          onCollectionChanged -= value;
       }
    }
    // Insert additional code.
}
<DefaultEvent("CollectionChanged")> _ 
Public Class MyCollection
    Inherits BaseCollection

    Public Event CollectionChanged (ByVal sender As Object, _
        ByVal e As CollectionChangeEventArgs)
    
    ' Insert additional code.
End Class

下一個範例會建立的 MyCollection實例。 然後,它會取得 類別的屬性、擷取 DefaultEventAttribute,並列印預設事件的名稱。

int main()
{
    // Creates a new collection.
    DefaultEventAttributeExample::TestCollection^ newCollection = 
        gcnew DefaultEventAttributeExample::TestCollection;
    
    // Gets the attributes for the collection.
    AttributeCollection^ attributes = 
        TypeDescriptor::GetAttributes(newCollection);
    
    // Prints the name of the default event by retrieving the 
    // DefaultEventAttribute from the AttributeCollection.
    DefaultEventAttribute^ attribute = (DefaultEventAttribute^)
        attributes[DefaultEventAttribute::typeid];
    Console::WriteLine("The default event is: {0}", attribute->Name);
}
public static int Main() {
    // Creates a new collection.
    MyCollection myNewCollection = new MyCollection();
 
    // Gets the attributes for the collection.
    AttributeCollection attributes = TypeDescriptor.GetAttributes(myNewCollection);
 
    /* Prints the name of the default event by retrieving the 
     * DefaultEventAttribute from the AttributeCollection. */
    DefaultEventAttribute myAttribute = 
       (DefaultEventAttribute)attributes[typeof(DefaultEventAttribute)];
    Console.WriteLine("The default event is: " + myAttribute.Name);
    return 0;
 }
Public Shared Function Main() As Integer
    ' Creates a new collection.
    Dim myNewCollection As New MyCollection()
    
    ' Gets the attributes for the collection.
    Dim attributes As AttributeCollection = TypeDescriptor.GetAttributes(myNewCollection)
    
    ' Prints the name of the default event by retrieving the
    ' DefaultEventAttribute from the AttributeCollection. 
    Dim myAttribute As DefaultEventAttribute = _
        CType(attributes(GetType(DefaultEventAttribute)), DefaultEventAttribute)
    Console.WriteLine(("The default event is: " & myAttribute.Name))
    Return 0
End Function 'Main

備註

Name使用屬性來取得預設事件的名稱。

如需詳細資訊,請參閱屬性

建構函式

DefaultEventAttribute(String)

初始化 DefaultEventAttribute 類別的新執行個體。

欄位

Default

指定 DefaultEventAttribute 的預設值,就是 null。 這個 static 欄位是唯讀的。

屬性

Name

取得與這個屬性繫結的元件的預設事件名稱。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,表示指定的物件值是否等於目前的 DefaultEventAttribute

GetHashCode()

傳回這個執行個體的雜湊碼。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱