.net c#使用roslyn的替换语句

Replace statement using roslyn
2021-06-10
  •  译文(汉语)
  •  原文(英语)

我正在尝试更新文件中的陈述.我的要求是更新

protected override void OnLaunchApplication(LaunchActivatedEventArgs args)
    {
        NavigationService.Navigate("Hub", null);
    }

protected override void OnLaunchApplication(LaunchActivatedEventArgs args)
    {
        NavigationService.Navigate("Main", null);
    }

我正在使用以下代码:

 // read file contents
            string fileContent = File.ReadAllText(targetFile);

            // create syntax tree
            SyntaxTree tree = SyntaxTree.ParseText(fileContent);

            // locate OnLaunchApplication event
            var targetMethod =
                tree.GetRoot()
                    .DescendantNodes()
                    .OfType<MethodDeclarationSyntax>()
                    .FirstOrDefault(x => (string)x.Identifier.Value == "OnLaunchApplication");

            if (targetMethod != null)
            {
                StatementSyntax oldSyntax = targetMethod.DescendantNodes()
                    .OfType<StatementSyntax>()
                    .FirstOrDefault(x => x.ToString() == @"NavigationService.Navigate(""Hub"", null);");

                StatementSyntax newHubSyntax = Syntax.ParseStatement(@"NavigationService.Navigate(""Main"", null);");

                CompilationUnitSyntax newRoot = tree.GetRoot().ReplaceNode(oldSyntax, newHubSyntax);

                File.WriteAllText(targetFile, newRoot.ToFullString());
            }

但以上结果是:

 protected override void OnLaunchApplication(LaunchActivatedEventArgs args)
    {
NavigationService.Navigate("Main", null);        }

该语句从左开始,甚至"}"括号也位于同一行.有什么建议?

解决过程1

您需要将SyntaxTrivia语句从旧语句复制到新语句.

顺便说一句:最好对要替换的节点进行较弱的检查,并仅替换LiteralExpressionSyntax"Hub"的节点.否则,原件中的任何空白都会使您感到混乱,并且您将在要替换的语句中删除任何其他格式/注释/等.

I am trying to update a statment in my file. my requirement is to update

protected override void OnLaunchApplication(LaunchActivatedEventArgs args)
    {
        NavigationService.Navigate("Hub", null);
    }

to

protected override void OnLaunchApplication(LaunchActivatedEventArgs args)
    {
        NavigationService.Navigate("Main", null);
    }

I am using following code:

 // read file contents
            string fileContent = File.ReadAllText(targetFile);

            // create syntax tree
            SyntaxTree tree = SyntaxTree.ParseText(fileContent);

            // locate OnLaunchApplication event
            var targetMethod =
                tree.GetRoot()
                    .DescendantNodes()
                    .OfType<MethodDeclarationSyntax>()
                    .FirstOrDefault(x => (string)x.Identifier.Value == "OnLaunchApplication");

            if (targetMethod != null)
            {
                StatementSyntax oldSyntax = targetMethod.DescendantNodes()
                    .OfType<StatementSyntax>()
                    .FirstOrDefault(x => x.ToString() == @"NavigationService.Navigate(""Hub"", null);");

                StatementSyntax newHubSyntax = Syntax.ParseStatement(@"NavigationService.Navigate(""Main"", null);");

                CompilationUnitSyntax newRoot = tree.GetRoot().ReplaceNode(oldSyntax, newHubSyntax);

                File.WriteAllText(targetFile, newRoot.ToFullString());
            }

but the result of the above is:

 protected override void OnLaunchApplication(LaunchActivatedEventArgs args)
    {
NavigationService.Navigate("Main", null);        }

the statement starts from the left and even "}" bracket comes on the same line. Any suggestions?

Solutions1

You will need to copy the SyntaxTrivia from the old statement to the new one.

BTW: It would probably be better to do a weaker check for the node you want to replace, and to replace only the LiteralExpressionSyntax for "Hub". Otherwise any white space in the original will mess you up, and you will remove any other formatting/comments/etc in the statement you are replacing.

转载于:https://stackoverflow.com/questions/19095362/replace-statement-using-roslyn

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入