Roslyn SyntaxTree 更改注入 [英] Roslyn SyntaxTree changes injection

查看:48
本文介绍了Roslyn SyntaxTree 更改注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了我的类 MonitorSyntaxRewriter,它继承自 CSharpSyntaxRewriter.通过这个课程,我改变了我的 SyntaxTree.但是,我怎样才能在某处注入"这个修改后的 synaxtree 呢?我的意思是,我在 Visual Studio 中有一些随机项目,在 Build 上,我希望所有的语法树都通过这个 MonitorSyntaxRewriter.有什么选择吗?

I wrote my class MonitorSyntaxRewriter, which inherits from CSharpSyntaxRewriter. With this class I change my SyntaxTree. But, how can I "inject" this modified synaxtree somewhere? I mean, I have some random project in Visual Studio, and on Build, I would like all the syntax trees go through this MonitorSyntaxRewriter. Is there some option for that?

或者有其他可能的解决方法吗?(创建新的解决方案......).我只是不希望我的 *.cs 文件在项目中被更改.

Or is there some other possible workaround? (creating new solution...). I just don't want my *.cs files being changed in the project.

推荐答案

据我所知,在编译并发送到磁盘之前,您无法插入构建过程并重写语法树.

As far as I know you can't plug into the build process and rewrite the syntax trees before they're compiled and emitted to disk.

这意味着实现您的愿望并不容易.但是,您的项目并非不可能,您可以想象创建自己的 Visual Studio 扩展,将菜单选项添加到 Visual Studio 并启动您自己的构建和发出过程.

This means achieving what you'd like is not going to be easy. However, your project isn't impossible and you could conceivably create your own Visual Studio extension that added a menu option to Visual Studio and kicked off your own build and emit process.

为了重写语法树并将它们应用到解决方案中,您需要将它们应用到它们的父文档中.在编写 Visual Studio 扩展时,您需要访问 VisualStudioWorkspace.这包含当前打开的解决方案中的解决方案、项目和文档.我写了一些 背景关于您可能感兴趣的工作区的信息.

In order to rewrite syntax trees and apply them to a solution, you need to apply them to their parent document. When writing Visual Studio extensions, you'll want to get access to the VisualStudioWorkspace. This contains the solution, project and documents within the currently opened solution. I've written some background info on workspaces you might be interested in.

您可以通过以下方式在 MEF 导出类中 MEF 导入 Visual Studio 工作区:

You can MEF import the Visual Studio Workspace within a MEF exported class via:

[Import(typeof(Microsoft.VisualStudio.LanguageServices.VisualStudioWorkspace))]

一旦您可以访问 VisualStudioWorkspace,您就可以一一重写每个文档.以下示例代码应该可以帮助您入门:

Once you have access to the VisualStudioWorkspace, you could rewrite each document one-by-one. The following sample code should get you started:

Workspace ws = null; //Normally you'd get access to the VisualStudioWorkspace here.
var currentSolution = ws.CurrentSolution;

foreach (var projectId in currentSolution.ProjectIds)
{
    var project = currentSolution.GetProject(projectId);
    foreach (var documentId in project.DocumentIds)
    {
        Document doc = project.GetDocument(documentId);
        var root = await doc.GetSyntaxRootAsync();

        //Rewrite your root here
        var rewrittenRoot = RewriteSyntaxRoot(root);

        //Save the changes to the current document
        doc = doc.WithSyntaxRoot(root);
        //Persist your changes to the current project
        project = doc.Project;
    }
    //Persist the project changes to the current solution
    currentSolution = project.Solution;
}

//Now you have your rewritten solution. You can emit the projects to disk one by one if you'd like.

这不会修改用户的代码,但允许您将自定义项目发送到磁盘或 MemoryStream,您可以将其加载到 AppDomain 或运行直接取决于你想要做什么.

This won't modify the user's code, but will allow you to emit your custom projects to disk or to a MemoryStream which you could load into an AppDomain or run directly depending on what you're trying to do.

这篇关于Roslyn SyntaxTree 更改注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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