In many scenario, you will want to generate fields in an existing class of an existing assembly. Let's imagine for example that you want to do some lazy loading in your classes. As a consequence, in all of your properties you will have some code checking if your private backing field has already been loaded or not. For that, you will have two solutions : look the nullity of your backing field, or look a boolean variable "hasBeenLoaded". This second solution is the best. Indeed the nullity can be a valid value and retreiving a "null" from your database shall prevent you from doing another get in your database.
Of course, this solution will result in a lots of cumbersome code that you will need to propagate in all of your classes. So you will probably think "aspect" and so use an AOP framework like PostSharp to do all of this plumbing code. You will find many posts on Internet speaking of dealing with LazyLoading functionality in PostSharp, but they usually deal with the nullity of the backing field.
So how can we use PostSharp (I will use the 1.5 version for this example. The version 2.0 of PostSharp will probably simplify this solution) to generate a boolean field ?
This example (that should be seen more like a proof of concept) will probably make you think of many other cases where you will need to generate some code in an existing class. Here we will generate a simple variable in a class, but the code can be easily adapted to generate a field per existing property.
[Plus]