BCL mscorlib [00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ] 1.0.5000.0 2.0.0.0 4.0.0.0 All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe. System.Attribute System.AttributeUsage(System.AttributeTargets.Class) System.Runtime.InteropServices.ComVisible(true)

When you are defining your own attribute class, you can control the manner in which it is used by placing an on your attribute class. The indicated attribute class must derive from , either directly or indirectly.

Attribute classes have positional and named parameters. Each public constructor for an attribute class defines a valid sequence of positional parameters for that class. Named parameters are defined by the non-static, public, and read-write fields or properties of the attribute class.

The three properties of are set by defining the following parameters:

This positional parameter specifies the program elements that the indicated attribute can be placed on. The set of all possible elements that you can place an attribute on is listed in the enumeration. You can combine several values using a bitwise OR operation to get the desired combination of valid program elements.

This named parameter specifies whether the indicated attribute can be specified more than once for a given program element.

This named parameter specifies whether the indicated attribute can be inherited by derived classes and overriding members.

For more information about using attributes, see and [<topic://cpconExtendingMetadataUsingAttributes>].

Specifies the usage of another attribute class. This class cannot be inherited.

- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

  You can combine several values using a bitwise OR operation to get the desired combination of valid program elements.

  For default property values, see the , , and properties.

  Initializes a new instance of the class with the specified list of , the value, and the value.

  The set of values combined using a bitwise OR operation to indicate which program elements are valid.
  0
- Property
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean

  A where indicates more than one instance of the attribute is permitted to be applied; otherwise, . The default is .

  The following example demonstrates the use of . If for an attribute is set to , more than one of those attributes can be assigned to any given program element.

  `using System; [AttributeUsageAttribute( AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true )] public class Author : Attribute { public Author(string name) { this.name = name; } public string name; } [Author( "John Doe" )] [Author( "John Q Public" )] class JohnsClass { public static void Main() {} }`

  The following example demonstrates an error that is expected to be caught by compilers: the sample attempts to assign multiple instances of an attribute for which was set to .

  `using System; [AttributeUsageAttribute( AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false )] public class Author : Attribute { public Author(string name) { this.name = name; } public string name; } [Author( "John Doe" )] [Author( "John Q Public" )] class JohnsClass { public static void Main() {} }`

  This should throw an error similar to:

  error CS0579: Duplicate 'Author' attribute

  An attribute that can be specified more than once for a program element is called a multi-use attribute. An attribute that can be specified only once is called a single-use attribute.

  Gets or sets a Boolean value indicating whether more than one instance of the indicated attribute can be specified for a single program element.

  0
- Property
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean

  indicates the attribute is inherited by subclasses; otherwise, . The default is .

  Information on an inherited attribute will be included in the metadata for the class on which it is applied, but will not be included in the metadata for classes that derive from it. A metadata consumer (such as reflection) is required therefore to traverse up the inheritance chain of a class if that consumer is interested in data that is marked inherited, but applied to an ancestor class. There is nothing for the compiler to validate at compile time.

  Gets or sets a Boolean value indicating whether the indicated attribute can be inherited by derived classes and overriding members.

  0
- Property
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.AttributeTargets

  One or more of the values sent to the constructor, combined by a bitwise OR operation.

  This property is read-only.

  Gets a set of values identifying which program elements that the indicated attribute can be applied to.

  0
0