How to install Roslyn (for use in source-code-modification)?

心已入冬 提交于 2021-02-18 03:26:49

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/33597741/how-to-install-roslyn-for-use-in-source-code-modification

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!