Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I don't think this is possible, but I have always hoped it is.

When you apply [Obsolete("You really shouldn't use this anymore.")] to a member, it causes the compiler to generate a warning or error, which then appears in the Error List in Visual Studio.

It would be amazing to be able to do this when building components for conditions other obsolescence, even something like: [Experimental("This method is still in development and should not be used in production environments.")] and the like.

Anyone?

FYI: System.ObsoleteAttribute is sealed.
Posted
Updated 5-Dec-13 4:21am
v2

1 solution

Unfortunately there is no magic in the ObsoleteAttribute[^] class. The compiler service looks for this attribute and displays the warnings in the dialog box.

However, you can raise warnings yourself, like:

C#
public void MyMethod()
{
#warning This method is still in development and is not recommended for production.
}


See #warning[^]
 
Share this answer
 
v3
Comments
Yvan Rodrigues 5-Dec-13 10:21am    
You know, I think I knew of #warning and #error years ago, and had completely forgotten about them. It doesn't solve my problem, since I (the component creator) will get the warning, not the end-user, but thanks for reminding me about this.
Ron Beyer 5-Dec-13 10:25am    
It really is too bad there isn't something like a WarningAttribute, but the recognition of the ObsoleteAttribute is purely compiler based and not something built into .NET. There are ways to suppress code analysis messages through attributes, but no way to raise warnings through them.
Ron Beyer 5-Dec-13 10:27am    
Another alternative is to use the ///summary XML tags to display a message to the user, since this shows up when they use intellisense. Something like:

///<summary>[Experimental] This method does something magical</summary>
public void MyMethod() { }
Yvan Rodrigues 5-Dec-13 10:29am    
nice tip!
BillWoodruff 5-Dec-13 20:02pm    
+5 Valuable information that had ... uhh ... slipped me mind: thanks, Ron

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900