Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an Attribute to Break the Build

OK, this kind of follows on from my previous question.

What I would really like to do is create some sort of attribute which allows me to decorate a method that will break the build. Much like the Obsolete("reason", true) attribute, but without falsely identifying obsolete code.

To clarify: I dont want it to break the build on ANY F6 (Build) press, I only want it to break the build if a method decorated with the attribute is called somewhere else in the code. Like I said, similar to obsolete, but not the same.

I know I am not alone in this, since other users want to use it for other reasons. I have never created custom attributes before so it is all new to me!

like image 740
Rob Cooper Avatar asked Aug 26 '08 14:08

Rob Cooper


2 Answers

I think this would be an excellent feature request for Microsoft: Create an abstract base class attribute CompilerExecutedAttribute that the compiler processes in some manner or that can influence the compiling process. Then we could inherit from this attribute and implement different operations, e.g. emit an error or a warning.

like image 143
Konrad Rudolph Avatar answered Oct 11 '22 21:10

Konrad Rudolph


If this is for XML serialization and NHibernate, where you want the parameterless constructor to be accessible (as is the case in the example you referenced), then use a private or protected parameterless constructor for serialization, or a protected constructor for NHibernate. With the protected version, you are opening yourself up to inherited classes being able to call that code.

If you don't want code calling a method, don't make it accessible.

EDIT: To perhaps answer the deeper question, AFAIK the compiler only knows about three attributes: Obsolete, Conditional, and AttributeUsage. To add special handling for other attributes would require modifying the compiler.

like image 35
Tom Mayfield Avatar answered Oct 11 '22 21:10

Tom Mayfield