使用 Roslyn 替换方法节点 [英] Replacing a method node using Roslyn

查看:43
本文介绍了使用 Roslyn 替换方法节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在探索 Roslyn 时,我组合了一个小应用程序,该应用程序应包含跟踪语句作为 Visual Studio 解决方案中每个方法中的第一个语句.我的代码有问题,只更新第一种方法.

While exploring Roslyn I put together a small app that should include a trace statement as the first statement in every method found in a Visual Studio Solution. My code is buggy and is only updating the first method.

未按预期工作的行标有TODO"注释.请指教.

The line that is not working as expected is flagged with a "TODO" comment. Please, advise.

我也欢迎可以创建更精简/可读的解决方案的风格建议.

I also welcome style recommendations that would create a more streamlined/readable solution.

提前致谢.

...

    private void TraceBtn_Click(object sender, RoutedEventArgs e) {
        var myWorkSpace = new MyWorkspace("...Visual Studio 2012\Projects\Tests.sln"); 
        myWorkSpace.InjectTrace();
        myWorkSpace.ApplyChanges();
    }

...

using System;
using System.Linq;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
using Roslyn.Services;

namespace InjectTrace
{
    public class MyWorkspace
    {
    private string solutionFile;
    public string SolutionFile {
        get { return solutionFile; }
        set { 
            if (string.IsNullOrEmpty(value)) throw new Exception("Invalid Solution File");
            solutionFile = value;
        }
    }

    private IWorkspace loadedWorkSpace;
    public IWorkspace LoadedWorkSpace { get { return loadedWorkSpace; } }

    public ISolution CurrentSolution { get; private set; }
    public IProject CurrentProject { get; private set; }
    public IDocument CurrentDocument { get; private set; }
    public ISolution NewSolution { get; private set; }


    public MyWorkspace(string solutionFile) {
        this.SolutionFile = solutionFile;
        this.loadedWorkSpace = Workspace.LoadSolution(SolutionFile);
    }

    public void InjectTrace()
    {

        int projectCtr = 0;
        int documentsCtr = 0;
        int transformedMembers = 0;
        int transformedClasses = 0;
        this.CurrentSolution = this.LoadedWorkSpace.CurrentSolution;
        this.NewSolution = this.CurrentSolution;

        //For Each Project...
        foreach (var projectId in LoadedWorkSpace.CurrentSolution.ProjectIds)
        {
            CurrentProject = NewSolution.GetProject(projectId);

            //..for each Document in the Project..
            foreach (var docId in CurrentProject.DocumentIds)
            {
                CurrentDocument = NewSolution.GetDocument(docId);
                var docRoot = CurrentDocument.GetSyntaxRoot();
                var newDocRoot = docRoot;
                var classes = docRoot.DescendantNodes().OfType<ClassDeclarationSyntax>();
                IDocument newDocument = null;

                //..for each Class in the Document..
                foreach (var @class in classes) {
                    var methods = @class.Members.OfType<MethodDeclarationSyntax>();

                    //..for each Member in the Class..
                    foreach (var currMethod in methods) {
                        //..insert a Trace Statement
                        var newMethod = InsertTrace(currMethod);
                        transformedMembers++;
                        //TODO: PROBLEM IS HERE
                        newDocRoot = newDocRoot.ReplaceNode(currMethod, newMethod);                             
                    }
                    if (transformedMembers != 0) {
                        newDocument = CurrentDocument.UpdateSyntaxRoot(newDocRoot);
                        transformedMembers = 0;
                        transformedClasses++;
                    }
                }

                if (transformedClasses != 0) {
                    NewSolution = NewSolution.UpdateDocument(newDocument);
                    transformedClasses = 0;
                }

                documentsCtr++;

            }
            projectCtr++;
            if (projectCtr > 2) return;
        }
    }

    public MethodDeclarationSyntax InsertTrace(MethodDeclarationSyntax currMethod) {
        var traceText =
        @"System.Diagnostics.Trace.WriteLine(""Tracing: '" + currMethod.Ancestors().OfType<NamespaceDeclarationSyntax>().Single().Name + "." + currMethod.Identifier.ValueText + "'\");";
        var traceStatement = Syntax.ParseStatement(traceText);
        var bodyStatementsWithTrace = currMethod.Body.Statements.Insert(0, traceStatement);
        var newBody = currMethod.Body.Update(Syntax.Token(SyntaxKind.OpenBraceToken), bodyStatementsWithTrace,
                                            Syntax.Token(SyntaxKind.CloseBraceToken));
        var newMethod = currMethod.ReplaceNode(currMethod.Body, newBody);
        return newMethod;

    }

    public void ApplyChanges() {
        LoadedWorkSpace.ApplyChanges(CurrentSolution, NewSolution);
    }


}

}

推荐答案

你代码的根本问题是 newDocRoot = newDocRoot.ReplaceNode(currMethod, newMethod); 以某种方式重建 newDocRoot 代码的内部表示,因此不会在其中找到下一个 currMethod 元素,并且下一个 ReplaceNode 调用将不执行任何操作.这种情况类似于在其 foreach 循环中修改集合.

The root problem of you code is that newDocRoot = newDocRoot.ReplaceNode(currMethod, newMethod); somehow rebuilds newDocRoot internal representation of code so next currMethod elements won't be find in it and next ReplaceNode calls will do nothing. It is a situation similar to modifying a collection within its foreach loop.

解决方案是收集所有必要的更改并使用 ReplaceNodes 方法立即应用它们.而这实际上自然会导致代码的简化,因为我们不需要跟踪所有这些计数器.我们只需存储所有需要的转换并立即将它们应用于整个文档.

The solution is to gather all necessary changes and apply them at once with ReplaceNodes method. And this in fact naturally leads to simplification of code, because we do not need to trace all those counters. We simply store all needed transformation and apply them for whole document at once.

更改后的工作代码:

public void InjectTrace()
{
    this.CurrentSolution = this.LoadedWorkSpace.CurrentSolution;
    this.NewSolution = this.CurrentSolution;

    //For Each Project...
    foreach (var projectId in LoadedWorkSpace.CurrentSolution.ProjectIds)
    {
        CurrentProject = NewSolution.GetProject(projectId);
        //..for each Document in the Project..
        foreach (var docId in CurrentProject.DocumentIds)
        {
            var dict = new Dictionary<CommonSyntaxNode, CommonSyntaxNode>();
            CurrentDocument = NewSolution.GetDocument(docId);
            var docRoot = CurrentDocument.GetSyntaxRoot();
            var classes = docRoot.DescendantNodes().OfType<ClassDeclarationSyntax>();

            //..for each Class in the Document..
            foreach (var @class in classes)
            {
                var methods = @class.Members.OfType<MethodDeclarationSyntax>();

                //..for each Member in the Class..
                foreach (var currMethod in methods)
                {
                    //..insert a Trace Statement
                    dict.Add(currMethod, InsertTrace(currMethod));
                }
            }

            if (dict.Any())
            {
                var newDocRoot = docRoot.ReplaceNodes(dict.Keys, (n1, n2) => dict[n1]);
                var newDocument = CurrentDocument.UpdateSyntaxRoot(newDocRoot);
                NewSolution = NewSolution.UpdateDocument(newDocument);
            }
        }
    }
}

这篇关于使用 Roslyn 替换方法节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆