简体   繁体   中英

Roslyn CodeFixProvider gives VS 2015 error

I have created a roslyn CodeAnalyzer and a CodeFixProvider.

The Analyzer runs fine, and creates the rule, but when I'm trying to open the popup showing the fix, I get a "One or more errors occurred" VS popup.

First time I ran it, it worked fine, but then I stopped debugging and after that it gave me that error, so I tried on another computer and again it worked fine the first time I 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 achive is

[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 it is here that VS2015 gives the error, just not first time...

不知道为什么,但我发现删除%localappdata%\\Microsoft\\VisualStudio\\14.0Exp文件夹有帮助。

Not sure what was wrong, since it only failed in debug. looked like a VS bug when running in debug

It also happened with me. I tried to find an answer in other forums with no luck. My workaround was incrementing the vsixminifest Version number.

Hope it helps. Thanks, Wilsade

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM