Created
October 20, 2016 15:21
-
-
Save semuserable/0573e3f39e2fb6a657d1575160e10625 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Text; | |
using Microsoft.CodeAnalysis; | |
using Microsoft.CodeAnalysis.CSharp; | |
using Microsoft.CodeAnalysis.CSharp.Syntax; | |
namespace ConsoleRoslyn_1 | |
{ | |
class MyTokenWalker : CSharpSyntaxWalker | |
{ | |
public MyTokenWalker() : base(SyntaxWalkerDepth.Token) { } | |
private StringBuilder sb = new StringBuilder(); | |
public string Result => sb.ToString(); | |
public override void VisitMethodDeclaration(MethodDeclarationSyntax node) | |
{ | |
sb.Append(node.ToString()); | |
base.VisitMethodDeclaration(node); | |
} | |
} | |
class MyRewriter : CSharpSyntaxRewriter | |
{ | |
public override SyntaxNode VisitIfStatement(IfStatementSyntax node) | |
{ | |
var body = node.Statement; | |
var block = SyntaxFactory.Block(body); | |
var newIf = node.WithStatement(block); | |
return newIf; | |
//return base.VisitIfStatement(node); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
FiddlingWithSemantics(); | |
} | |
static void FiddlingWithIncrementingIntByOne() | |
{ | |
var tree = CSharpSyntaxTree.ParseText(@" | |
class Temp | |
{ | |
void Foo() | |
{ | |
int num = 5; | |
string str = ""hello""; | |
Console.WriteLine(num); | |
} | |
} | |
"); | |
var root = tree.GetRoot(); | |
} | |
static void FiddlingWithSemantics() | |
{ | |
var tree = CSharpSyntaxTree.ParseText(@" | |
public partial class MyPartialClass | |
{ | |
void MyMethod() | |
{ | |
// (int, int) Get() => (1,1); | |
System.Console.WriteLine(""Hello World""); | |
} | |
} | |
public partial class MyPartialClass | |
{ | |
}"); | |
var mscorlib = MetadataReference.CreateFromFile(typeof (object).Assembly.Location); | |
var compilation = CSharpCompilation.Create("MyCompilation", | |
syntaxTrees: new [] {tree}, references: new [] {mscorlib}); | |
var root = tree.GetRoot(); | |
var semanticModel = compilation.GetSemanticModel(tree); | |
var methodSyntax = root.DescendantNodes().OfType<MethodDeclarationSyntax>().First(); | |
var methodSymbol = semanticModel.GetDeclaredSymbol(methodSyntax); | |
var parentAssembly = methodSymbol.ContainingAssembly; | |
Console.WriteLine(methodSyntax.ReturnType.GetType().Name); | |
Console.WriteLine(parentAssembly); | |
} | |
static void FiddlingWithTree() | |
{ | |
var tree = CSharpSyntaxTree.ParseText(@" | |
class Temp | |
{ | |
void Foo() | |
{ | |
} | |
} | |
"); | |
var root = tree.GetRoot(); // CompilationUnitSyntax (blue) | |
var method = root.DescendantNodes() | |
.OfType<MethodDeclarationSyntax>().First(); | |
var syntaxFactory = SyntaxFactory.Identifier("Foo2"); | |
var methodNew = method.WithIdentifier(syntaxFactory); | |
var newRoot = root.ReplaceNode(method, methodNew); | |
//method.ReplaceNode(methodNew,) | |
Console.WriteLine(root); | |
Console.WriteLine(newRoot); | |
} | |
static void FiddlingWithRewriter() | |
{ | |
var tree = CSharpSyntaxTree.ParseText(@" | |
class Temp | |
{ | |
void Foo() | |
{ | |
if(true) | |
Console.WriteLine(""Yeap""); | |
if(true) | |
Console.WriteLine(""Yeap2""); | |
} | |
} | |
"); | |
var root = tree.GetRoot(); | |
var rewriter = new MyRewriter(); | |
var newRoot = rewriter.Visit(root); | |
Console.WriteLine(newRoot.ToFullString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment