The Roslyn C# gives developers access to the C# language parsing and generation capabilities. This can be leveraged in many ways by building tools that fully understand the code and are capable of generating new code with full comprehension of your codebase. In this talk I will walk you through the basic concepts of the Roslyn and language parsing, building a small tool using these principles and packaging it as a .NET Core tool for easy distribution and usage via the .NET Core CLI
15. Symbol building creates a semantic model
Symbols know:
● Where they are declared
● What namespace they belong to
● Is it abstract? Sealed?
● Does it hide a base method?
● What symbol does it return?
36. Binding & Flow Analysis API
● Go To Definition
● Extract method
● Rename
● Find All References
● Autocompletion
● Signature assistance
● So much more!
42. Demo 1: Syntax analysis
using System;
using System.Collections;
using System.Linq;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
43. Demo 1: Syntax analysis
We have:
● Turned code into a syntax tree
dynamically
● Explored manually the content
of the tree
● Queried the tree using LINQ
44. Demo 2: Semantic analysis
using System;
using System.Collections;
using System.Linq;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
45. Demo 2: Semantic analysis
We have:
● Turned code into a syntax tree dynamically
● Built a semantic model of our code
● Queried the semantic model about:
○ Our code
○ Dependencies
46. Demo 3: Basic syntax transformation
using System;
using System.Collections; -> using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
47. Demo 3: Basic syntax transformation
We have:
● Created a new syntax node from scratch
● Manually found the target to replace in
the syntax tree
● Made a new tree, replacing the target node
with our new node
48. Demo 4: Advanced syntax transformation
This: MyType myVariable = new
MyType();
To this: var myVariable = new
MyType();
In the scope of a project!
49. Demo 4: Advanced syntax transformation
We have:
● Read code from .cs files
● Dynamically identified the nodes to
replace
● Built up new nodes, keeping the original
semantics intact
● Replaced the target nodes in the syntax
tree
● Turned our syntax tree into .cs files,
replacing the old ones!