O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

28.5. The AttributeUsage attribute

It's about time we examined the AttributeUsage attribute in greater detail. After all, AttributeUsage is the special attribute you apply to all your attribute classes so that the C# compiler knows how you want your attribute to work.

A class that is tagged with AttributeUsage must be a subclass of System.Attribute, or else a compilation error will occur. The AttributeUsage attribute specification can take in three parameters – one positional (and hence mandatory) and two named ones:

  • AttributeTarget – an int to indicate which types and class entities this attribute class can be applied to (this parameter is mandatory [5]);

    [5] Take it that the AttributeUsageAttribute class itself has only one constructor which ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required