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
and AnalyzeNode
methods with (replace the namespace with your own):
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(AnalyzeSyntaxNode, SyntaxKind.InvocationExpression);
}
private void AnalyzeSyntaxNode(SyntaxNodeAnalysisContext context)
{
InvocationExpressionSyntax invocationExpression = context.Node as InvocationExpressionSyntax;
IMethodSymbol methodSymbol = context.SemanticModel.GetSymbolInfo(invocationExpression).Symbol as IMethodSymbol;
if (
methodSymbol != null
&& methodSymbol.Name == "ReplaceNewline"
&& methodSymbol.ContainingNamespace.Name == "MySampleFix"
&& methodSymbol.OriginalDefinition.Parameters.Length == 1)
{
if (invocationExpression.ArgumentList.Arguments.Count() == 1)
{
LiteralExpressionSyntax arg =
invocationExpression.ArgumentList.Arguments[0].Expression as LiteralExpressionSyntax;
if (arg != null && arg.Token.ValueText == "|")
{
Diagnostic.Create(Rule, invocationExpression.GetLocation());
}
}
}
}
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
and Regex.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 a Debug.Assert(input != "|");
in the method, that way developers will get early warning (at development/debug time) that they're using your API incorrectly.