AttributeTargets Énumération
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Spécifie les éléments d’application sur lesquels il est valide pour appliquer un attribut.
Cette énumération prend en charge une combinaison au niveau du bit de ses valeurs membres.
public enum class AttributeTargets
[System.Flags]
public enum AttributeTargets
[System.Flags]
[System.Serializable]
public enum AttributeTargets
[System.Flags]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum AttributeTargets
[<System.Flags>]
type AttributeTargets =
[<System.Flags>]
[<System.Serializable>]
type AttributeTargets =
[<System.Flags>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type AttributeTargets =
Public Enum AttributeTargets
- Héritage
- Attributs
Champs
| Nom | Valeur | Description |
|---|---|---|
| Assembly | 1 | L’attribut peut être appliqué à un assembly. |
| Module | 2 | L’attribut peut être appliqué à un module.
|
| Class | 4 | L’attribut peut être appliqué à une classe. |
| Struct | 8 | L’attribut peut être appliqué à une structure ; c’est-à-dire un type valeur. |
| Enum | 16 | L’attribut peut être appliqué à une énumération. |
| Constructor | 32 | L’attribut peut être appliqué à un constructeur. |
| Method | 64 | L’attribut peut être appliqué à une méthode. |
| Property | 128 | L’attribut peut être appliqué à une propriété. |
| Field | 256 | L’attribut peut être appliqué à un champ. |
| Event | 512 | L’attribut peut être appliqué à un événement. |
| Interface | 1024 | L’attribut peut être appliqué à une interface. |
| Parameter | 2048 | L’attribut peut être appliqué à un paramètre. |
| Delegate | 4096 | L’attribut peut être appliqué à un délégué. |
| ReturnValue | 8192 | L’attribut peut être appliqué à une valeur de retour. |
| GenericParameter | 16384 | L’attribut peut être appliqué à un paramètre générique. Actuellement, cet attribut ne peut être appliqué qu’en C#, Microsoft langage intermédiaire (MSIL) et au code émis. |
| All | 32767 | L’attribut peut être appliqué à n’importe quel élément d’application. |
Exemples
L’exemple suivant illustre l’application d’attributs à différentes cibles.
Note
Visual Basic syntaxe ne prend pas en charge l'application d'attributs pour taper des paramètres.
using System;
namespace AttTargsCS {
// This attribute is only valid on a class.
[AttributeUsage(AttributeTargets.Class)]
public class ClassTargetAttribute : Attribute {
}
// This attribute is only valid on a method.
[AttributeUsage(AttributeTargets.Method)]
public class MethodTargetAttribute : Attribute {
}
// This attribute is only valid on a constructor.
[AttributeUsage(AttributeTargets.Constructor)]
public class ConstructorTargetAttribute : Attribute {
}
// This attribute is only valid on a field.
[AttributeUsage(AttributeTargets.Field)]
public class FieldTargetAttribute : Attribute {
}
// This attribute is valid on a class or a method.
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
public class ClassMethodTargetAttribute : Attribute {
}
// This attribute is valid on a generic type parameter.
[AttributeUsage(AttributeTargets.GenericParameter)]
public class GenericParameterTargetAttribute : Attribute {
}
// This attribute is valid on any target.
[AttributeUsage(AttributeTargets.All)]
public class AllTargetsAttribute : Attribute {
}
[ClassTarget]
[ClassMethodTarget]
[AllTargets]
public class TestClassAttribute {
[ConstructorTarget]
[AllTargets]
TestClassAttribute() {
}
[MethodTarget]
[ClassMethodTarget]
[AllTargets]
public void Method1() {
}
[FieldTarget]
[AllTargets]
public int myInt;
public void GenericMethod<
[GenericParameterTarget, AllTargets] T>(T x) {
}
static void Main(string[] args) {
}
}
}
open System
// This attribute is only valid on a class.
[<AttributeUsage(AttributeTargets.Class)>]
type ClassTargetAttribute() =
inherit Attribute()
// This attribute is only valid on a method.
[<AttributeUsage(AttributeTargets.Method)>]
type MethodTargetAttribute() =
inherit Attribute()
// This attribute is only valid on a constructor.
[<AttributeUsage(AttributeTargets.Constructor)>]
type ConstructorTargetAttribute() =
inherit Attribute()
// This attribute is only valid on a field.
[<AttributeUsage(AttributeTargets.Field)>]
type FieldTargetAttribute() =
inherit Attribute()
// This attribute is valid on a class or a method.
[<AttributeUsage(AttributeTargets.Class ||| AttributeTargets.Method)>]
type ClassMethodTargetAttribute() =
inherit Attribute()
// This attribute is valid on a generic type parameter.
[<AttributeUsage(AttributeTargets.GenericParameter)>]
type GenericParameterTargetAttribute() =
inherit Attribute()
// This attribute is valid on any target.
[<AttributeUsage(AttributeTargets.All)>]
type AllTargetsAttribute() =
inherit Attribute()
[<ClassTarget>]
[<ClassMethodTarget>]
[<AllTargets>]
type TestClassAttribute [<ConstructorTarget>] [<AllTargets>] () =
[<FieldTarget>]
[<AllTargets>]
let myInt = 0
[<MethodTarget>]
[<ClassMethodTarget>]
[<AllTargets>]
member _.Method1() = ()
member _.GenericMethod<[<GenericParameterTarget; AllTargets>] 'T>(x: 'T) = ()
Module DemoModule
' This attribute is only valid on a class.
<AttributeUsage(AttributeTargets.Class)> _
Public Class ClassTargetAttribute
Inherits Attribute
End Class
' This attribute is only valid on a method.
<AttributeUsage(AttributeTargets.Method)> _
Public Class MethodTargetAttribute
Inherits Attribute
End Class
' This attribute is only valid on a constructor.
<AttributeUsage(AttributeTargets.Constructor)> _
Public Class ConstructorTargetAttribute
Inherits Attribute
End Class
' This attribute is only valid on a field.
<AttributeUsage(AttributeTargets.Field)> _
Public Class FieldTargetAttribute
Inherits Attribute
End Class
' This attribute is valid on a class or a method.
<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Method)> _
Public Class ClassMethodTargetAttribute
Inherits Attribute
End Class
' This attribute is valid on any target.
<AttributeUsage(AttributeTargets.All)> _
Public Class AllTargetsAttribute
Inherits Attribute
End Class
<ClassTarget, _
ClassMethodTarget, _
AllTargets> _
Public Class TestClassAttribute
<ConstructorTarget, _
AllTargets> _
Public Sub New
End Sub
<MethodTarget, _
ClassMethodTarget, _
AllTargets> _
Public Sub Method1()
End Sub
<FieldTarget, _
AllTargets> _
Public myInt as Integer
End Class
Sub Main()
End Sub
End Module
Remarques
La AttributeUsageAttribute classe utilise cette énumération pour spécifier le type d’élément sur lequel il est valide pour appliquer un attribut.
AttributeTargets Les valeurs d’énumération peuvent être combinées avec une opération OR au niveau du bit pour obtenir la combinaison préférée.