简体   繁体   English

Roslyn 语法重写器是如何集成的?

[英]How are Roslyn syntax rewriters integrated?

I'd like to use a Roslyn syntax rewriter in an existing .NET Core project.我想在现有的 .NET Core 项目中使用 Roslyn 语法重写器。 However, I'm unsure how to go about integrating it.但是,我不确定如何整合它。 For analysis, I have a separate NuGet package that gets referenced by the project;为了分析,我有一个单独的 NuGet 包被项目引用; this works well enough.这工作得很好。 However, I can't find any documentation on how to integrate a syntax rewriter with the existing workflow.但是,我找不到有关如何将语法重写器与现有工作流集成的任何文档。

Since this is a .NET Core project, I'm currently compiling using both Visual Studio and the dotnet command line tool.由于这是一个 .NET Core 项目,我目前正在使用 Visual Studio 和dotnet命令行工具进行编译。 The project is a csproj referencing netcoreapp3.0 (preview).该项目是一个引用 netcoreapp3.0(预览版)的 csproj。 I'd like to integrate my syntax rewriter such that whenever I run dotnet build or press F5/F6 in Visual Studio, the syntax rewriter runs.我想集成我的语法重写器,这样每当我在 Visual Studio 中运行dotnet build或按 F5/F6 时,语法重写器都会运行。 While there's a lot of documentation for building a syntax rewriter, there doesn't seem to be any for integrating it into the build process.虽然有很多用于构建语法重写器的文档,但似乎没有将其集成到构建过程中的文档。

To be clear, here's what I'm trying to do:需要明确的是,这就是我想要做的:

  1. I will eventually have a syntax rewriter based on this documentation .我最终将有一个基于此文档的语法重写器。 It transforms the code at compile-time.它在编译时转换代码。
  2. I have a C# project with code that I would like to transform.我有一个包含要转换的代码的 C# 项目。
  3. I would like to take the syntax rewriter from step #1 and run it automatically when I compile the project from step #2.我想从步骤 #1 中获取语法重写器,并在我从步骤 #2 编译项目时自动运行它。

For example, say I have the following code:例如,假设我有以下代码:

class A
{
    [SomeAttribute]
    void Method()
    {
    }
}

I can easily make a syntax rewriter that takes this code in the form of a syntax tree, alters it, and produces code corresponding to the following syntax tree:我可以轻松地制作一个语法重写器,以语法树的形式获取此代码,对其进行更改,并生成与以下语法树对应的代码:

class A
{
    [SomeAttribute]
    [AnotherAttribute]
    void Method()
    {
        SomeCode();
    }
}

Optionally, I can produce a syntax tree by taking the code as a string input, as well as render the final syntax tree to a string.或者,我可以通过将代码作为字符串输入来生成语法树,并将最终的语法树呈现为字符串。 That's easy enough.这很容易。

What I can't find instructions for is automating this process.我找不到说明是自动化这个过程。 I don't want to have to manually run an EXE each time I build;我不想每次构建时都手动运行 EXE; I want to be able to integrate it into the default build process, and I'm looking for the correct way to do that.我希望能够将它集成到默认构建过程中,我正在寻找正确的方法来做到这一点。

Ideally, this would also involve rewriting the syntax tree live in Visual Studio as the developer writes code.理想情况下,这还涉及在开发人员编写代码时在 Visual Studio 中实时重写语法树。 This would permit IntelliSense to operate on the final syntax tree, rather than what the developer has written.这将允许 IntelliSense 对最终的语法树进行操作,而不是开发人员编写的内容。 However, I don't know if this part is possible to achieve.但是,我不知道这部分是否可以实现。

Syntax rewriters are not things that are part of the build.语法重写器不是构建的一部分。 They are typically used to build code fixes that are run inside Visual Studio.它们通常用于构建在 Visual Studio 中运行的代码修复。

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

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