views:

154

answers:

3

Hi,

there are attributes in .NET (e.g. ObsoleteAttribute) which are able to prevent compilation or at least throw compile time warnings depending on their configuration.

How to write an attribute that is able to interfere with compilation? Is there a way to do this from outside Microsoft?

+2  A: 

Using PostSharp: http://stackoverflow.com/questions/1420143/generating-a-custom-compile-time-warning-c/1420161#1420161

Frederik Gheysels
PostSharp is a great hint, thanks.
zzandy
+2  A: 

Obsolete attribute is "compiler magic" - you can't do something like this on your own without modifying the compiler. However, as always, there are workarounds - e.g. take a look at this and this thread.

Saulius
A: 

Any attributes that exhibit this inside the compiler are special cases. The [Obsolete] behaviour is described in the C# spec (17.4.3), for example.

You could write a custom FxCop rule?

Marc Gravell
That doesn't give you the opportunity to give you a compile time error/warning, does it ?
Frederik Gheysels
It does if you include FxCop in your build process. In VS2008 (team developer edition) you can do this either by declaring a build symbol, or clicking a checkbox on project properties...
Marc Gravell
For info, it is the CODE_ANALYSIS symbol, or the checkbox on the "Code Analysis" tab.
Marc Gravell