I would like to make the C# compiler check more at compile time. For example:
public class Foo
{
public bool condition;
public void CheckCondition()
{
if(condition) {
#warning SomeCompilerWarning
}
}
}
static void Main()
{
var foo = new Foo();
foo.condition = true;
foo.CheckCondition();
}
This trivial sequence should be able to be detected compile time. I realize #warning
is a pre-processor directive. I would like the above to somehow generate a warning. Are there any ways to slightly extend the compile time checks for trivial sequences or are custom extensions the only way?
Clarifications:
- I'm asking for an alternative to the above that does not use the pre-processor. The above is an example in concept what I would like to achieve.
- I want the compiler to emit a warning similar to the example. The example doesn't work. It is just used as an illustration.
- Rephrase: Are there any alternative to writing a custom analyzer to achieve the behavior in the example, without using any preprocessor directive? The
#warning
is just an illustration as to what I would like to happen.