Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to throw compilation error from an attribute?

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?

like image 998
zzandy Avatar asked Sep 15 '09 08:09

zzandy


3 Answers

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.

like image 135
Saulius Valatka Avatar answered Sep 17 '22 14:09

Saulius Valatka


Using PostSharp: Generating a custom compile time warning C#

like image 33
Frederik Gheysels Avatar answered Sep 19 '22 14:09

Frederik Gheysels


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?

like image 35
Marc Gravell Avatar answered Sep 18 '22 14:09

Marc Gravell