Creating Custom Attributes

You can also create your own custom attributes for storing metadata in a module or assembly (but not as new compiler directives). To create a new attribute, you use (surprise!) an attribute—the AttributeUsage attribute:

[AttributeUsage(
  validon,
  AllowMultiple=allowmultiple,
  Inherited=inherited
)]

Here are the parts of this attribute:

  • validon— Gives the language element(s) with which the attribute can be used. This item is a combination of AttributeTargets values (see Table 14.1), ORed together. The default value is AttributeTargets.All.

  • allowmultiple (optional)— If true, the attribute is multiuse, which means you can use it multiple times for the same element. Default is false (single-use).

  • inherited (optional)— ...

Get Microsoft® Visual C#® .NET 2003 Kick Start now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.