6

If I want to declare a method in my code as deprecated / obsolete, I can add the [Obsolete] attribute to it and make the compiler emit a warning (or error) whenever the method is used.

Is it possible to achieve a similar effect for third-party methods (such as System.Console.WriteLine)? Obviously, I cannot add the attribute since I do not control the code. But maybe there is some other trick available in .NET or Visual Studio?

I'm preferably looking for an "out of the box" solution that does not require something like writing my own post-build script that manually parses the code.

Sebastian Negraszus
  • 11,915
  • 7
  • 43
  • 70
  • 1
    nothing out of the box exists like that. resharper has code annotations. im guessing you could do something w/ roslyn. – Daniel A. White Jan 27 '16 at 15:26
  • 2
    Sounds more like you need code inspections with a custom rule. It's not really down to you to decide whether something else is obsolete, but you _can_ decide if you don't want to call it. – James Thorpe Jan 27 '16 at 15:28
  • With Visual Studio 2015, you can write a code analyzer fairly easily and quickly and mark certain methods as "not recommended" and throw up a warning or error at compile time. See: https://msdn.microsoft.com/en-us/magazine/dn879356.aspx – willaien Jan 27 '16 at 15:35
  • @JamesThorpe Well, it's obsolete within our project, but yes, it's ultimately about not calling it in our code. "Code inspection" is a good keyword. I may end up writing a custom code inspection rule for ReSharper... – Sebastian Negraszus Jan 27 '16 at 15:47
  • Cant do much about static types, but for others you could inherit and use new keyword to hide base implementation. Sometimes this is a good choice. – nawfal Mar 16 '18 at 10:26

3 Answers3

7

With Visual Studio 2015 you can create live Code Analyzers that can provide custom design-time checking for virtually anything. A good tutorial is available here. These usually live as part of the solution, so so they will "follow it around" no matter where it is compiled.

Code analyzers can can raise compile time errors or warnings, and can even present a UI to automatically correct the issue. They can be VERY powerful, but writing one of these can be fairly complex depending on what you need.

A similar feature exists for previous versions of Visual Studio (2010+). It isn't as well integrated, but might work for you.

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
  • Nice. Sadly, I'm currently stuck with VS 2013, but this answer may help others. – Sebastian Negraszus Jan 27 '16 at 15:49
  • I believe you can create something similar for earlier version of VS, but they are run only at compile time. The don't give you the same live feedback. Trying to find more information for you now. – Bradley Uffner Jan 27 '16 at 15:57
  • Check this out: http://blogs.msdn.com/b/codeanalysis/archive/2010/03/26/how-to-write-custom-static-code-analysis-rules-and-integrate-them-into-visual-studio-2010.aspx – Bradley Uffner Jan 27 '16 at 15:58
3

You can do that by creating custom code inspection rules in ReSharper.

Go to ReSharper / Options / Code Inspection / Custom Pattern / Add Pattern, write a pattern that matches the deprecated method call and select an inspection severity such as "suggestion" or "warning". You may also write a replacement pattern that can be applied via quick fixes.

Example:

Example of one custom pattern

In this example, the System.Console was misused for logging and should be replaced by proper log4net calls.

Sebastian Negraszus
  • 11,915
  • 7
  • 43
  • 70
1

Another option for ReSharper users: ExternalAnnotations.

Here is an example of adding the annotations for Selenium's WebDriver.dll:

  1. Create ExternalAnnotations folder beside *.csproj of the target project.
  2. Inside the folder create WebDriver.xml:
    <?xml version="1.0" encoding="utf-8" ?> 
    <assembly name="WebDriver">
      <member name="M:OpenQA.Selenium.INavigation.GoToUrl(System.String)">
        <attribute ctor="M:System.ObsoleteAttribute.#ctor(System.String,System.Boolean)">
          <argument>Use different overload of this method.</argument>
          <argument>true</argument>
        </attribute>
      </member>
    
  3. Reload the solution.
Monsignor
  • 2,671
  • 1
  • 36
  • 34