Condividi tramite


DefaultEventAttribute Classe

Definizione

Specifica l'evento predefinito per un componente.

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
Ereditarietà
DefaultEventAttribute
Attributi

Esempio

Nell'esempio seguente viene definita una classe di raccolta denominata MyCollection. La classe è contrassegnata con un DefaultEventAttribute oggetto che specifica CollectionChanged come evento predefinito.

[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
{
    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

Nell'esempio seguente viene creata un'istanza di MyCollection. Ottiene quindi gli attributi per la classe , estrae DefaultEventAttributee stampa il nome dell'evento predefinito.

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();

    // 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

Commenti

Utilizzare la Name proprietà per ottenere il nome dell'evento predefinito.

Per altre informazioni, vedere Attributi.

Costruttori

Nome Descrizione
DefaultEventAttribute(String)

Inizializza una nuova istanza della classe DefaultEventAttribute.

Campi

Nome Descrizione
Default

Specifica il valore predefinito per , DefaultEventAttributeovvero null. Questo static campo è di sola lettura.

Proprietà

Nome Descrizione
Name

Ottiene il nome dell'evento predefinito per il componente a cui è associato questo attributo.

TypeId

Se implementato in una classe derivata, ottiene un identificatore univoco per questo Attribute.

(Ereditato da Attribute)

Metodi

Nome Descrizione
Equals(Object)

Restituisce un valore che indica se il valore dell'oggetto specificato è uguale all'oggetto corrente DefaultEventAttribute.

GetHashCode()

Restituisce il codice hash per questa istanza.

GetType()

Ottiene il Type dell'istanza corrente.

(Ereditato da Object)
IsDefaultAttribute()

Quando sottoposto a override in una classe derivata, indica se il valore di questa istanza è il valore predefinito per la classe derivata.

(Ereditato da Attribute)
Match(Object)

Quando sottoposto a override in una classe derivata, restituisce un valore che indica se questa istanza è uguale a un oggetto specificato.

(Ereditato da Attribute)
MemberwiseClone()

Crea una copia superficiale del Objectcorrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Implementazioni dell'interfaccia esplicita

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

Esegue il mapping di un set di nomi a un set corrispondente di ID dispatch.

(Ereditato da Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Recupera le informazioni sul tipo per un oggetto, che può essere utilizzato per ottenere le informazioni sul tipo per un'interfaccia.

(Ereditato da Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Recupera il numero delle interfacce di informazioni sul tipo fornite da un oggetto (0 o 1).

(Ereditato da Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Fornisce l'accesso alle proprietà e ai metodi esposti da un oggetto .

(Ereditato da Attribute)

Si applica a

Vedi anche