<p>OK, this kind of follows on from <a href="https://stackoverflow.com/questions/27758/notify-developer-of-a-do-not-use-method">my previous question</a>.</p>

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

<p><strong>To clarify</strong>: I dont want it to break the build on <em>ANY</em> 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, <em>similar</em> to obsolete, but not the same.</p>

<p>I know I am not alone in this, since <a href="https://stackoverflow.com/questions/27758/notify-developer-of-a-do-not-use-method#27796">other users want to use it for other reasons</a>. I have never created custom attributes before so it is all new to me!</p>

[{'answer_id': 28153, 'author': 'Ross', 'author_id': 2025, 'author_profile': 'https://Stackoverflow.com/users/2025', 'pm_score': 0, 'selected': False, 'text': '<p>This is all starting to sound a bit like <a href="http://thedailywtf.com/Articles/SUSBSTITUTE_CODE.aspx" rel="nofollow noreferrer">Yesterday\'s TDWTF</a>. :-)</p>\n'}, {'answer_id': 28161, 'author': 'Tom Mayfield', 'author_id': 2314, 'author_profile': 'https://Stackoverflow.com/users/2314', 'pm_score': 2, 'selected': False, 'text': '<p>If you consider a warning (which is what [Obsolete] throws up) build-breaking, then just use the <a href="http://msdn.microsoft.com/en-us/library/963th5x3.aspx" rel="nofollow noreferrer">#warning</a> compiler directive.</p>\n\n<p>Edit: I\'ve never used it, but <a href="http://msdn.microsoft.com/en-us/library/x5hedts0.aspx" rel="nofollow noreferrer">#error</a> is also available.</p>\n'}, {'answer_id': 28191, 'author': 'Greg Hurlman', 'author_id': 35, 'author_profile': 'https://Stackoverflow.com/users/35', 'pm_score': -1, 'selected': False, 'text': '<p>Why not just make something up?  An unknown attribute would surely break the build.</p>\n\n<pre><code>[MyMadeUpAttributeThatBreaksTheBuildForSure]\npublic class NotDoneYet {}\n</code></pre>\n'}, {'answer_id': 28210, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 0, 'selected': False, 'text': "<p>I will have to agree with Greg: make up an attribute for it. </p>\n\n<p>And if you're really serious, maybe find a way to figure out if the constructor is being accessed by anything other than XMLSerializer and throw an exception if it is.</p>\n"}, {'answer_id': 28220, 'author': 'Tom Mayfield', 'author_id': 2314, 'author_profile': 'https://Stackoverflow.com/users/2314', 'pm_score': 3, 'selected': True, 'text': '<p>If this is for XML serialization and NHibernate, where you want the parameterless constructor to be accessible (as is the case in the <a href="https://stackoverflow.com/questions/27758/notify-developer-of-a-do-not-use-method#27796">example</a> 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.</p>\n\n<p>If you don\'t want code calling a method, don\'t make it accessible.</p>\n\n<p>EDIT: To perhaps answer the deeper question, AFAIK the compiler only knows about three attributes: <a href="http://msdn.microsoft.com/en-us/library/aa664620(VS.71).aspx" rel="nofollow noreferrer">Obsolete, Conditional, and AttributeUsage</a>. To add special handling for other attributes would require modifying the compiler.</p>\n'}, {'answer_id': 28242, 'author': 'Jorge Córdoba', 'author_id': 2695, 'author_profile': 'https://Stackoverflow.com/users/2695', 'pm_score': 0, 'selected': False, 'text': '<p>I\'d suggest you to use the #error directive.</p>\n\n<p>Another pretty unknown attribute that might do the work is the <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.conditionalattribute.aspx" rel="nofollow noreferrer">conditional attribute</a> (depending on what you\'re trying to ahieve)</p>\n\n<pre><code>[Conditional("CONDITION")] \npublic static void MiMethod(int a, string msg)\n</code></pre>\n\n<p>which will remove the method invocation from the IL code itself if "MY_CONDITION" is defined.</p>\n'}, {'answer_id': 28254, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 0, 'selected': False, 'text': "<p>Create an FxCop rule, and add FxCop to your integration build in order to check for this.</p>\n\n<p>You'll get warnings, rather than a failing build.  Attributes 'run' at reflection time rather than build time.</p>\n\n<p>Alternatively (and this is rather nasty) put a compiler directive around the method you don't want to be called. Then your code will break if you call it, but you can set up a build that passes the right compiler directive and doesn't.</p>\n"}, {'answer_id': 29095, 'author': 'Vaibhav', 'author_id': 380, 'author_profile': 'https://Stackoverflow.com/users/380', 'pm_score': 1, 'selected': False, 'text': '<p>I think the only fool proof way would be to extend the Visual Studio (through VSIP) and subscribe to the correct event (maybe in the EnvDTE.BuildEvents) class, and check your code for usage of the constructor, and cancel the build if you detect it.</p>\n'}, {'answer_id': 43746, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 3, 'selected': False, 'text': '<p>I think this would be an excellent feature request for Microsoft: Create an abstract base class attribute <code>CompilerExecutedAttribute</code> 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.</p>\n'}, {'answer_id': 10930348, 'author': 'ChrisFletcher', 'author_id': 172000, 'author_profile': 'https://Stackoverflow.com/users/172000', 'pm_score': 0, 'selected': False, 'text': '<p>Throw a custom exception and unit test for it as a post build step</p>\n'}, {'answer_id': 11857068, 'author': 'Dean Lunz', 'author_id': 341706, 'author_profile': 'https://Stackoverflow.com/users/341706', 'pm_score': 0, 'selected': False, 'text': '<p>Responding 4 years later :) </p>\n\n<p>I had same question if there were an alternative to Obsolete. </p>\n\n<p>From what I recall (channel9 videos) a little while ago Microsoft stated that it is working on giving devs access to something like a compiler api at some point so in the future it\'s conceivable that you could write a compiler "plugin" that would allow to to decorate methods with your own custom attribute and tell the compiler to cancel if it looks like the decorated code could be getting called some where else in the code etc. </p>\n\n<p>Which would actually be pretty cool when you think about it. It also reminds me that I should also try and read up on the progress of that compiler api thing MS is working on ...</p>\n'}]
['https://Stackoverflow.com/questions/28150', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/832/']