2

I want to mark a method as Obsolete, and also cause the compilation to fail if it is called from anywhere.

I came across a solution here How do I mark a method as Obsolete/Deprecated? .

an answer suggests this syntax, saying that the boolean modifier will achieve my wanted effect(failing compilation)

 [Obsolete("Method1 is deprecated, please use Method2 instead.", true)]

However, while this worked on the same project; it didn't when calling the method from another project (i even had visual studio productivity power tools show an eror for it but compilation still succeeded)

is this by design? or is there a workaround?

Community
  • 1
  • 1
Souhaieb Besbes
  • 1,485
  • 17
  • 30
  • 1
    Read [this](http://stackoverflow.com/questions/10380916/why-obsolete-causes-error) SO thread. Simply turn on "Treat warnings as errors". – sszarek Apr 24 '15 at 09:19
  • 11
    If you want code that references this method to not compile, why have it in the first place? Delete it from your API. The [`Obsolete`] attribute is used to discourage (not forbid) users from using this method, while maintaing backwards compatibility. If that's not what you want, then `Obsolete` is not what you need. – dcastro Apr 24 '15 at 09:19
  • 1
    The use of obsolete methods will generate warnings. You could turn on warnings as errors on your projects for these specific warnings. – Kevin D Apr 24 '15 at 09:20
  • @dcastro you should post that as an answer – pyrocumulus Apr 24 '15 at 09:42
  • Are you sure you're *rebuilding* the other project? `[Obsolete]` should throw warnings when building a new project that references the method, but existing projects that were already built and referenced that method shouldn't be affected. – Theodoros Chatzigiannakis Apr 24 '15 at 09:47
  • @Theodoros Chatzigiannakis i have the two projects in the same solution, and i was tinkering by adding and removing the boolean value, but that didn't prevent compilation. – Souhaieb Besbes Apr 24 '15 at 09:54
  • 1
    @dcastro i'm not sure that microsoft developers share your point of view, because the true boolean does just that (forbid the use), my problem is that it doesn't work between projects. ' why have it in the first place?' the problem is i know that code is obsolete, and i won't use it, but in two years another deveoper may be tempted to use it, and won't pay attention to a simple warning. – Souhaieb Besbes Apr 24 '15 at 09:57
  • 1
    Why not just make it `private` and mark it? – Tomer Apr 24 '15 at 10:00
  • may main question is if this behavior is by design or a possible bug – Souhaieb Besbes Apr 24 '15 at 10:02
  • @SouhaiebBesbes You are absolutely right to ask. `[Obsolete("", true)]` **should** throw a compiler error, even across projects. This is what the documentation says and this is the behavior I'm seeing right now on my system. May I suggest you search for any directives in your project that suppress the error? – Theodoros Chatzigiannakis Apr 24 '15 at 10:03
  • @sszarek this doesn't solve my problem since the syntax already produces an error, but only for references from the same project in my case – Souhaieb Besbes Apr 24 '15 at 10:11
  • @SouhaiebBesbes Also, have you referenced the one project as an external assembly to the other, or as a project reference within the solution? If you have done the former, then may be accidentally referencing an out-of-date version of your other project for some reason. Try the latter as well. – Theodoros Chatzigiannakis Apr 24 '15 at 10:12
  • @Theodoros Chatzigiannakis it's a project reference so no out-of-date problem here – Souhaieb Besbes Apr 24 '15 at 10:20
  • 1
    @SouhaiebBesbes If you set up a new solution with two new projects, can you reproduce this issue? Does it still ignore the attribute or does it throw the error in that case? – Theodoros Chatzigiannakis Apr 24 '15 at 10:22
  • @Theodoros Chatzigiannakis in a new solution this works fine, so i'm giving up – Souhaieb Besbes Apr 24 '15 at 10:32

1 Answers1

2

If you use something marked as obsolete inside a method or class which is also marked as obsolete, the compiler will give you no warning or error.

Consider the following obsolete method in some class:

public class SomeClass
{
    [Obsolete("Don't use",true)]
    public static void ObsoleteMethod() { }
}

The expected behavior is that it yields a compiler error whenever it is used.

But if you use it in another obsolete method, you not even get a compiler warning.

public class AnotherClass
{
    public void Method()
    {
        SomeClass.ObsoleteMethod();  // Compiler error
    }

    [Obsolete("Avoid use",false)]
    public void AnotherObsoleteMethod()
    {
        SomeClass.ObsoleteMethod(); // No error and no warning
    }
}

This is true also if the whole class is marked as obsolete:

[Obsolete()]
public class ObsoleteClass
{
    public void Method()
    {
        SomeClass.ObsoleteMethod(); // No error
    }
}
abto
  • 1,583
  • 1
  • 12
  • 31
  • After checking my code, i can confirm that was the situation i was facing. this explains why the attribute worked as expected when i tested it in dummy project. This makes me wonder why this kind of behaviour isn't documented anywhere, or is it? – Souhaieb Besbes Mar 10 '16 at 08:51
  • I didn't find anything about this, I stumbled across this some time ago while I was refactoring a library. – abto Mar 10 '16 at 18:44