3

In c# there are attributes such as [obsolete] that create compiler warnings that will be shown in visual studio.

Is there an attribute that I can use to mark a method or a class with a comment that should be shown as a warning in visual studio when I compile?

Something like:

[TBD(Msg="Please change me after 2010 07 20")]
public void Foo(){
}

or is there a possibility that I can derive from System.Attribute and make my own attribute, configuring visual studio so that it behaves as I described.

UPDATE

Thanks to all for your answers I have accepted the answer from Robaticus because he showed me the solution that I really needed:

#warning Message

shows the desired message that I wanted and it does this without any overhead. Thanks!

However I searched in the wrong direction and therefore asked the wrong question. For those who are reading this post and are interested in an answer to the initial question, I find the link that Mark Rushakoff has posted is very interesting. In this post, Pablo Fernandez has shown a clever way using an attribute on an attribute to accomplish what I have asked for.

Community
  • 1
  • 1
HCL
  • 36,053
  • 27
  • 163
  • 213

2 Answers2

7

Use the second parameter to do this.

//Compiling error
[System.Obsolete("Obsolete use blah instead", true)]


//Compiling warning
[System.Obsolete("Obsolete use blah instead", false)]
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
3

Another option is do do a preprocessor directive:

#warning Please change this code after 2010 07 20
Robaticus
  • 22,857
  • 5
  • 54
  • 63