Roslyn CodeFixProvider gives error VS 2015

I created roslyn CodeAnalyzer and CodeFixProvider.

The analyzer works fine and creates a rule, but when I try to open a pop-up window indicating the correction, I get "One or more errors occurred . " VS popup

The first time I ran it, it worked fine, but then I stopped debugging, after which it gave me this error, so I tried on another computer, and again it worked fine the first time it was debugged.

My analyzer:

private static void Analyze(SyntaxNodeAnalysisContext context) { var localDeclaration = (LocalDeclarationStatementSyntax)context.Node; foreach (var variable in localDeclaration.Declaration.Variables) { var initializer = variable.Initializer; if (initializer == null) return; } var node = context.Node; while (node.Kind() != SyntaxKind.MethodDeclaration) { node = node.Parent; } var method = (MethodDeclarationSyntax)node; if (method.AttributeLists.Any(x => x.Attributes.Any(y => y.Name is IdentifierNameSyntax && ((IdentifierNameSyntax)y.Name).Identifier.Text.ToLower().Contains("test")))) { context.ReportDiagnostic(Diagnostic.Create(Rule, context.Node.GetLocation())); } } 

My CodeFixProvider

 private async Task<Document> AddAssertionsAsync(Document document, LocalDeclarationStatementSyntax localDeclaration, CancellationToken cancellationToken) { var editor = await DocumentEditor.CreateAsync(document, cancellationToken); var assert = SyntaxFactory.IdentifierName("Assert"); var areEqual = SyntaxFactory.IdentifierName("AreEqual"); var memberAccess = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, assert, areEqual); var firstArgument = SyntaxFactory.Argument(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(@""))); var secondArgument = SyntaxFactory.Argument(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(@""))); var argumentList = SyntaxFactory.SeparatedList<ArgumentSyntax>( new SyntaxNodeOrToken[] { firstArgument, SyntaxFactory.Token(SyntaxKind.CommaToken), secondArgument }); var assertToken = SyntaxFactory.ExpressionStatement( SyntaxFactory.InvocationExpression(memberAccess, SyntaxFactory.ArgumentList(argumentList))); editor.InsertAfter(localDeclaration, assertToken); var newDocument = editor.GetChangedDocument(); return newDocument; } 

What I'm trying to achieve

 [Test] public void blah() { var stat = string.Empty; } 

becomes

 [Test] public void blah() { var stat = string.Empty; Assert.AreEqual("", ""); } 

When you press ctrl +. on "stat" ... And this is where VS2015 gives an error, not the first time ...

+5
source share
3 answers

Not sure what was wrong, as it fell away only in debugging. looked like a VS error when running in debug

0
source

I don’t know why, but I found that deleting the %localappdata%\Microsoft\VisualStudio\14.0Exp helps.

+2
source

It also happened to me. I tried to find the answer in other forums with no luck. My workaround increased the number of vsixminifest Version versions.

Hope this helps. Thanks, Wilsade

0
source

Source: https://habr.com/ru/post/1261570/


All Articles