Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a third-party method [Obsolete]

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.

like image 259
Sebastian Negraszus Avatar asked Jan 27 '16 15:01

Sebastian Negraszus


People also ask

What is an obsolete method?

What's an obsolete method? A method in a class library which is old or out-of-use and some other method instead of this is suggested to be used.

How do you mark a method as deprecated?

To mark a method as deprecated we can use the JavaDoc @deprecated tag. This is what we did since the beginning of Java. But when a new metadata support introduced to the Java language we can also use annotation. The annotation for marking method as deprecated is @Depreated .

How do you make a method deprecated in C#?

This attribute is found in the System namespace. The Obsolete attribute decorates a program element by putting the word “Obsolete” above it inside square brackets. Since it is an attribute, we can use either Obsolete or ObsoleteAttribute. [Obsolete] − is a no parameter constructor and is a default using this attribute.

What does obsolete attribute mean?

Obsolete attributes are used to display an error or warning during compilation with an optional message to alert the developer that the given type or its member should not be used in the code as it is going to be replaced.


3 Answers

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.

like image 191
Bradley Uffner Avatar answered Oct 31 '22 23:10

Bradley Uffner


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.

like image 30
Sebastian Negraszus Avatar answered Oct 31 '22 22:10

Sebastian Negraszus


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.
like image 45
Monsignor Avatar answered Oct 31 '22 21:10

Monsignor