简体   繁体   English

Roslyn SyntaxTree改变了注射

[英]Roslyn SyntaxTree changes injection

I wrote my class MonitorSyntaxRewriter, which inherits from CSharpSyntaxRewriter. 我编写了我的类MonitorSyntaxRewriter,它继承自CSharpSyntaxRewriter。 With this class I change my SyntaxTree. 通过这门课,我改变了我的SyntaxTree。 But, how can I "inject" this modified synaxtree somewhere? 但是,我怎样才能“注入”这个修改过的synaxtree? I mean, I have some random project in Visual Studio, and on Build, I would like all the syntax trees go through this MonitorSyntaxRewriter. 我的意思是,我在Visual Studio中有一些随机项目,在Build上,我希望所有语法树都通过这个MonitorSyntaxRewriter。 Is there some option for that? 那有什么选择吗?

Or is there some other possible workaround? 或者还有其他可能的解决方法吗? (creating new solution...). (创造新的解决方案......)。 I just don't want my *.cs files being changed in the project. 我只是不希望我的* .cs文件在项目中被更改。

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. 据我所知,你无法插入构建过程并在编译和发送到磁盘之前重写语法树。

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扩展,为Visual Studio添加一个菜单选项,并启动您自己的构建和发出进程。

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 . 编写Visual Studio扩展时,您将需要访问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. 我已经写了一些你可能感兴趣的工作空间的背景信息

You can MEF import the Visual Studio Workspace within a MEF exported class via: 您可以通过以下方式MEF导入MEF导出类中的Visual Studio工作区:

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

Once you have access to the VisualStudioWorkspace , you could rewrite each document one-by-one. 一旦您可以访问VisualStudioWorkspace ,就可以逐个重写每个文档。 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.

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. 这不会修改用户的代码,但允许您将自定义项目发送到磁盘或MemoryStream ,您可以将其加载到AppDomain或直接运行,具体取决于您尝试执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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