5

In c# I can mark some functions as obsolete by doing

[Obsolete]
void Foo()
{}

this tell the compiler to throw warning if I am using this function.

I want to create my own tag [Experimental] which just as obsolete throw a warning telling the user who build the code that this function is not well tested and using it may result in troubles.

Is it possible to do this? How?

Petr
  • 13,747
  • 20
  • 89
  • 144
  • As far as I remember you can pass a custom warning message to Obsolete attribute. You can mark the code as Obsolete but in the message tell that the code is experimental. – Artemix May 10 '13 at 14:34
  • http://msdn.microsoft.com/en-us/library/sw480ze8(v=vs.80).aspx – NT88 May 10 '13 at 14:35
  • I updated the header, so that it's more clear... seems that people are answering just how to create attributes and somebody were downvoting them for that. :/ – Petr May 10 '13 at 14:41

4 Answers4

16

I want to create my own attribute [Experimental] which just as [Obsolete] does makes the compiler produce a warning. Is it possible to do this?

No, sorry. Obsolete is a very special attribute. The compiler has special-purpose code written for it. There is no extensible warning mechanism in C#.

If you would like to submit this suggestion to the Visual Studio team, you can do that at http://visualstudio.uservoice.com/.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
3

The Obsolete attribute is special, as the C# compiler knows about it. You won't be able to create your own. The closest you could get would be to inherit from ObsoleteAttribute yourself, but the class is sealed so this is not possible.

As it is, you could mark your method as [Obsolete("This method is not well tested.")]. That's the best you can do.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
  • 1
    Downvoter care to explain? Unlike the other solutions, this *will* generate a compiler warning (or error, if the user chooses to so alter the base constructor call). – Adam Robinson May 10 '13 at 14:36
  • 2
    ObsoleteAttribute is marked as sealed (http://msdn.microsoft.com/en-us/library/system.obsoleteattribute.aspx), and cannot be inherited from. Also did not downvote...but that's probably why someone did. – Maciej May 10 '13 at 14:36
  • 6
    I didnt downvote, but ObsoleteAttribute is sealed, this wont compile – Jurica Smircic May 10 '13 at 14:37
  • this looks interesting... I would like to know why it got downvoted so many times though – Petr May 10 '13 at 14:37
  • @Maciej: Good catch (though I think you mean `sealed`; `final` is Java). I've altered the answer to reflect that. – Adam Robinson May 10 '13 at 14:38
  • 1
    @Petr: Possibly because the `ObsoleteAttribute` class is `sealed` (which I unwisely hadn't bothered to check before answering). – Adam Robinson May 10 '13 at 14:40
1

You can add it as an Attribute.

[System.AttributeUsage(AttributeTargets.All)]
public class Experimental: System.Attribute
{

}
Chris Dixon
  • 9,147
  • 5
  • 36
  • 68
1

I think you can do it with PostSharp. There was a post about it on SO, but I ohly have the bookmark link now:

http://fgheysels.blogspot.com/2008/08/locking-system-with-aspect-oriented.html

Look under Compile Time Validation in that page, it shows you how the author did it.

Further note: it involves a bit of plumbing code. Definitely not a "slap-it-in-to-the-project" variety of code.

code4life
  • 15,655
  • 7
  • 50
  • 82