The SLAR on System.Attribute

Continuing in the series on sharing some of the information in the .NET Framework Standard Library Annotated Reference Vol 1 here are some of the annotations on the

System.Attribute class.

 

Brad Abrams The ability to extend the metadata format in a structured way is an important

advance of the CLI. The development community has always had a need to add

declarative information to code. With the advent of custom attributes, we are no

longer limited to the glacially slow pace of adding new keywords to languages or the

ugly and error-prone pre-processor support for adding declarative markup to code.

Jeff Richter: I agree with Brad. Custom attributes is an incredibly useful and innovative feature.

I’m constantly coming up with new ways to apply them. For example, I’ve created

my own custom attribute to help me with command-line argument parsing. I

define a structure with fields and apply an instance of my attribute to each field. The

attribute indicates the command-line switch letter and I implemented a method that

can parse command-line arguments and set the appropriate fields in the structure.

Then, the rest of my code just accesses the structure’s fields. Adding new commandline

switches is a breeze. I’ve also used attributes to localize strings for enum values

and for defining strings to present for a user that correspond with programmatic values.

Creative use of custom