[Mono-list] Implementing attribute-based code generation

Stephen Touset stephen@touset.org
Thu, 13 Jan 2005 08:47:53 -0500


Shawn Vose wrote:

> Use System.Reflection to read available methods, attributes and 
> properties for a dll. You can use it to do so at runtime. I know there 
> has been a few questions and answers on Relection in the archives

What I'm trying to accomplish is slightly different, and cannot be done 
with the current design of attributes.

As an example, I want to be able to place preconditions and 
postconditions on methods and invariants on classes. These attributes 
need to follow inheritance rules for Design By Contract as well: 
postconditions are logically ORed with any inherited postconditions from 
base classes, while postconditions and invariants are logically ANDed 
with inherited ones from their base classes. This is not possible with 
the current implementation of attributes.

Nor is the ability for an attribute to consitently, automatically, and 
predictable modify the way a method or class works. Attributes cannot 
access the methods they belong to, nor can they generate code 
dynamically. There is also no way to have them transparently invoke code 
upon method execution.

Because of this, something would need to be written to have certain 
attributes generate code in the methods they modify, or in all the 
methods of the classes they modify. Also, these attributes would need to 
be inherited upon their base classes as well, automatically. A script 
could theoretically do it, but that would require a lot of additional 
work, reimplementing a C# parser that already exists with Mono.

If you can find a way to do this with System.Reflection, please let me 
know. It would save me a bundle of time.