What's New With Attributes in .NET 2.0?
I thought it would be worthwhile to take a look at what changes (if any) are going on with attributes in .NET 2.0. As I found out...well, nothing much has changed! The
ICustomAttributeProvider interface, the
Attribute class and the
AttributeUsageAttribute class have the same number of members (properties, methods, etc.) that they currently do in 1.1. The current Beta 1 docs do list more derived classes for
Attribute (335) then what is listed in the current docs (195). I wasn't expecting a lot of changes in the next version of .NET relative to classes that defined the core aspects of attributes but I felt it was worth at least a passing glance.
I haven't dived into the 2.0 classes to see if there's anything new relative to the CodeDOM classes or compilation in general. Chapter 5 in "Applied .NET Attributes" was kind of a pain to write because I wanted to enforce a rule expressed as an attribute at the compilation level. What I ended up doing was examing the binary via the FxCop classes post-build and ensuring that my rule was not violated. This required a whole new compiler that pretty much wrapped csc.exe. It didn't feel "right", but it was the best I could do. .NET has great support for metadata lookups at runtime but there's not much you can do to create a compile-time attribute (think
CLSCompliantAttribute). From my brief perusals it doesn't seem like anything has been added to extend the compilation process with attributes. It would be cool if there was direct support for this as it may be possible to eliminate some of the custom rules developers write in FxCop. By creating errors at compile-time, a developer would instantly know if they're doing something that the in-house architecture wants to enforce. Having this in a FxCop rule is good, but this requires a post-build step and it doesn't make the assembly "invalid" (a build may fail because of it but a straight compile in VS .NET would still work). Maybe there's something in 2.0 that I don't know about. If there is, that could eliminate a lot of the "programming-by-convention" tasks that sometimes occur on projects (i.e. "always make sure you class inherits from interface X" or "always call a virtual base class method if you override it"). Or maybe, just maybe, Phoenix may have something that supports the idea of an extensible compiler...
* Posted at 01.19.2005 04:19:51 PM CST | Link *