Have you ever heard of Boo? It has interesting ways in which you can hook up into the compiler pipeline. One such feature is called syntactic attributes, which are attributes that implement an interface that the compiler calls, so that they can participate in code generation.
class Person:
[getter(FirstName)]
_fname as string
[getter(LastName)]
_lname as string
def constructor([required] fname, [required] lname):
_fname = fname
_lname = lname
The attributes in this code will generate public getters for the fields and null checks for the constructor parameters. It will all end up in the compiled assembly.
I've always wanted this kind of extensibility to be part of the C# compiler. Maybe it will one day. Until then, you can use a post-compiler, like CciSharp. CCiSharp will rewrite the CIL based on special attributes in the assembly, just like with Boo synctatic attributes.
Given this code:
class Foo {
[Lazy]
public int Value {
get { return Environment.Ticks; }
}
}
CCiSharp will mutate the code based on the LazyAttribute
to this:
class Foo {
int Value$Value; // compiler generated
int Value$Initialized;
int GetValueUncached() {
return Environment.Ticks;
}
public int Value {
get {
if(!this.Value$Initialized) {
this.Value$Value = this.GetValueUncached();
this.Value$Initialized = true;
}
return this.Value$Value;
}
}
CCiSharp is based on the Common Compiler Infrastructure project, the same used to implement the code contracts post compiler in the upcoming .NET Framework 4.0.
So this is how you can mutate the generated CIL.
But, a #warning
directive does not have a CIL representation, it's a compiler directive only. To add this directive, what you must mutate is not the generated CIL, but the C# code itself. You'd have to implement a C# parser for that. I think the best option is, as stated in other responses, to create a post-build event that will reflect over the generated assembly and issue the desired warning.