AttributeTargets Výčet
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Určuje prvky aplikace, pro které je platné použít atribut.
Tento výčet podporuje bitové kombinace hodnot jeho členů.
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
- Dědičnost
- Atributy
Pole
| Name | Hodnota | Description |
|---|---|---|
| Assembly | 1 | Atribut lze použít na sestavení. |
| Module | 2 | Atribut lze použít pro modul.
|
| Class | 4 | Atribut lze použít u třídy. |
| Struct | 8 | Atribut lze použít na strukturu; to znamená typ hodnoty. |
| Enum | 16 | Atribut lze použít u výčtu. |
| Constructor | 32 | Atribut lze použít u konstruktoru. |
| Method | 64 | Atribut lze použít pro metodu. |
| Property | 128 | Atribut lze použít u vlastnosti. |
| Field | 256 | Atribut lze použít u pole. |
| Event | 512 | Atribut lze použít u události. |
| Interface | 1024 | Atribut lze použít na rozhraní. |
| Parameter | 2048 | Atribut lze použít u parametru. |
| Delegate | 4096 | Atribut lze použít u delegáta. |
| ReturnValue | 8192 | Atribut lze použít na návratovou hodnotu. |
| GenericParameter | 16384 | Atribut lze použít u obecného parametru. V současné době lze tento atribut použít pouze v jazyce C#, Microsoft zprostředkující jazyk (MSIL) a vygenerovaný kód. |
| All | 32767 | Atribut lze použít pro libovolný prvek aplikace. |
Příklady
Následující příklad znázorňuje použití atributů pro různé cíle.
Note
Visual Basic syntaxe nepodporuje použití atributů pro parametry typu.
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
Poznámky
Třída AttributeUsageAttribute používá tento výčet k určení typu elementu, na kterém je platný použít atribut.
AttributeTargets Hodnoty výčtu lze kombinovat s bitové operace OR, aby se získala upřednostňovaná kombinace.