I'm currently working on improving my coding sensation so I've started adding some extension methods to the types I'm using.
I figured out, that I'm doing the same action quite often always with the same attributes.
I want to show this hint when someone calls ReplaceNewLine("|")
:
The char you want to remove is
|
. Use theRemoveNewLine()
extension without any attributes instead.
I tried it with the [Obsolete(...)]
attribute, but this got shown every time I called the function.
My question is: How can I show a specific hint based on my input within Visual Studio?
Code:
public static class StringExtension
{
public static string ReplaceNewLine(this string s)
{
return s.Replace("|", Environment.NewLine);
}
// show hint if c is |
public static string ReplaceNewLine(this string s, string c)
{
return s.Replace(c, Environment.NewLine);
}
}
Apposition:
- Of course the hint may have the
Obsolete
code (0618
/CS0618
) when it is shown, but that's not important for me. I just want to get the hint shown! - I'm working with
C# 6.0
,.NET 4.6
andVisual Studio 2015 RC
.
In Visual Studio 2015 this is possible using a Roslyn Diagnostic (and optional Fix). The new Visual Studio 2015 code editor uses Roslyn under the hood to do all it's parsing an the Code Analaysis, Metrics and Refactoring engine is now based on it.
A sample implementation of such an inspection is given on the Roslyn github page. A full implementation would be a bit much for an answer here on StackOverflow, as it entails a number of steps to go through and amounts to a complete tutorial, but this full tutorial of something similar is given here. and may be the basis for your work. (ask additional questions later). Code for the standard rules that ship with the product can be found in the Roslyn GitHub as well.
This piece of code should get you pretty close, but I haven't tested it. Create a standard diagnostic & fix according to the Roslyn SDK totorial and replace the
Initialize
andAnalyzeNode
methods with (replace the namespace with your own):If you want to make something that's backwards compatible with older versions of Visual Studio you can opt to write a custom code analysis rule. This example rule takes the input to a call to
Regex.Match
andRegex.Replace
and gives a warning when it doesn't compile. It would be even simpler to give a warning when it is a constant string.Visual studio extensions like Resharper and CodeRush offer an SDK which can do things similar to FxCop, but they embed into the IDE like Roslyn does. It may be an option for you to take that approach.
If you want something in the code editor that doesn't use any extension or customization, then adding a
<remark />
to the codedoc is about as much as you can do. In the worst case you could put in aDebug.Assert(input != "|");
in the method, that way developers will get early warning (at development/debug time) that they're using your API incorrectly.