如何安装Roslyn(用于源代码修改)? [英] How to install Roslyn (for use in source-code-modification)?

查看:44
本文介绍了如何安装Roslyn(用于源代码修改)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想安装编译器-据我所知它已经安装.(我正在使用Visual Studio 2015).

我不需要源代码.我只希望能够进行一些简单的代码更改,例如

蓝色节点代表 SyntaxNode ,绿色节点表示 SyntaxToken . SyntaxNodes 是树的内部节点,可以分解为较小的部分. SyntaxTokens 是语法树的基本单位,不能分解成较小的部分.(您不能将 { public 分解为较小的内容).

还必须注意, SyntaxTree 不可变的.这意味着我们不能直接更改树.相反,我们必须在原始树的基础上创建一个新树,但要对其进行更改.

我们将所有内容放在一起并重命名一个方法:

  var tree = CSharpSyntaxTree.ParseText(@"类MyClass{无效的MyMethod(){}}");//我们通过获取根来导航这些树,然后//在树中上下搜索我们感兴趣的节点.var root = tree.GetRoot();var method = root.DescendantNodes().OfType< MethodDeclarationSyntax>().Single();//让我们创建一个不同名称的新方法var newIdentifier = SyntaxFactory.Identifier("MyNewMethodWithADifferentName");//注意:我们正在创建一棵新树,而不是更改旧树!var newMethod = method.WithIdentifier(newIdentifier);Console.WriteLine(newMethod); 

在这一点上,我们实际上只刮了一下表面.您还可以使用其他方法来重写源代码,包括:

  1. DocumentEditor -请参阅: https://stackoverflow.com/a/30563669/300908
  2. 注释(第235和239行)
  3. .TrackNodes()
  4. CSharpSyntaxRewriter >以自下而上的方式替换节点.我已经在博客上写过.

Roslyn API的表面积非常大.您可以使用成千上万种公共类型和方法.刚开始时可能会让人感到不知所措,但是我发现可以放心,我想对源代码做的几乎所有事情都可以通过Roslyn完成.这是一个非常强大的工具.

I don't want to install the compiler - as far as I know it's already installed. (I'm using Visual Studio 2015).

I don't want the source code. I just want to be able to do some simple code changing like this question asks about.

What I've found is mainly github which doesn't seem clear. And other sources seem to be quite outdated. Is there something there that I'm overlooking?

So - How do I get the tools I need for source-code-modifying using Roslyn?

解决方案

This is sort of a tough question to answer because you'll have to learn a number of things to go from "Installing Roslyn" to "Rewriting source code". There's no official documentation, but I've kept track of my progress as I've learned the API over at Learn Roslyn Now.

Important topics:

  • Installing Roslyn
  • The Roslyn Syntax Tree
  • Immutability

Roslyn is deployed as a NuGet package that you can install to a project via:

Install-Package Microsoft.CodeAnalysis

For more (including a video on installing helper tools) see Part 1: Installing Roslyn.

The Syntax Tree API is the single most important concept to understand for users new to Roslyn. As programmers, we're accustomed to dealing with source code in the form of strings that we manipulate directly. Behind the scenes, the compiler takes these strings and turns them into tree structures.

For example, the following code:

class SimpleClass
{
    public void SimpleMethod()
    {
    }
}

Is represented as the following syntax tree:

The blue nodes represent a SyntaxNode and the green nodes represent a SyntaxToken. SyntaxNodes are the internal nodes of the tree and can be broken down into smaller pieces. SyntaxTokens are the fundamental units of the syntax tree and cannot be broken down into smaller pieces. (You can't break { or public into anything smaller).

It's also important to note that a SyntaxTree is immutable. This means we can't change a tree directly. Instead, we have to create a new tree based off the original, but with our changes applied to it.

Let's put this all together and rename a method:

var tree = CSharpSyntaxTree.ParseText(@"
class MyClass
{
    void MyMethod()
    {
    }
}");

//We navigate these trees by getting the root, and then
//searching up and down the tree for the nodes we're interested in.
var root = tree.GetRoot();
var method = root.DescendantNodes().OfType<MethodDeclarationSyntax>().Single();

//Let's create a new method with a different name
var newIdentifier = SyntaxFactory.Identifier("MyNewMethodWithADifferentName");
//NOTE: We're creating a new tree, not changing the old one!
var newMethod = method.WithIdentifier(newIdentifier);
Console.WriteLine(newMethod);

At this point we've really only scratched the surface. There are a whole other host of way you can rewrite source code including:

  1. The DocumentEditor - See: https://stackoverflow.com/a/30563669/300908
  2. Annotations (Lines 235 and 239)
  3. .TrackNodes()
  4. The CSharpSyntaxRewriter that replaces nodes in a bottom-up approach. I've written about this on my blog.

The Roslyn API has an absolutely massive surface area. There are thousands of public types and methods available at your disposal. It can be very overwhelming at first, but I find it reassuring that almost anything I'd like to do to source code can be accomplished via Roslyn. It's a very powerful tool.

这篇关于如何安装Roslyn(用于源代码修改)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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