SlideShare une entreprise Scribd logo
1  sur  26
ROSLYN
What it is and how to get started
Oslo/NNUG
Tomas Jansson
27/05/2014
THIS IS ME
Tomas Jansson
Manager & Practice Lead .NET
BEKK Oslo
@TomasJansson
tomas.jansson@bekk.no
github.com/mastoj
blog.tomasjansson.com
AGENDA
What is Roslyn
Summary
The compile
pipeline
How to get
started
Existing projectsDemo
WHAT IS ROSLYN?
The .NET Compiler Platform ("Roslyn") provides open-source
C# and Visual Basic compilers with rich code analysis APIs.
You can build code analysis tools with the same APIs that
Microsoft is using to implement Visual Studio!
http://roslyn.codeplex.com/
WHAT IS ROSLYN?
The .NET Compiler Platform ("Roslyn") provides open-source
C# and Visual Basic compilers with rich code analysis APIs.
You can build code analysis tools with the same APIs that
Microsoft is using to implement Visual Studio!
http://roslyn.codeplex.com/
WHAT IS ROSLYN?
It’s a ”compiler-as-a-service (caas)”
WHAT IS A COMPILER-AS-A-SERVICE?
”C# compiler is like a box of chocolates,
you never know what you’re gonna get”
http://theawesomedaily.theawesomedaily.netdna-cdn.com/wp-content/uploads/2013/12/forrest-gump-original.jpeg
WHAT IS A COMPILER-AS-A-SERVICE?
”C# compiler is like a box of chocolates,
you never know what you’re gonna get”
This was life
before Roslyn
http://theawesomedaily.theawesomedaily.netdna-cdn.com/wp-content/uploads/2013/12/forrest-gump-original.jpeg
Now we know
exactly what
we gonna get!
WHY?
Power to the
developers
Easier to add
features (for
Microsoft)
THE COMPILE PIPELINE
http://roslyn.codeplex.com/wikipage?title=Overview&referringTitle=Home
HOW TO GET STARTED
Download and install the SDK: http://roslyn.codeplex.com/
Run ”Install Roslyn Preview into Roslyn Experimental Hive.exe”
Install all the Visual Studio Extensions
Roslyn End User Preview.vsix
Roslyn SDK Project Templates.vsix
Roslyn Syntax Visualizer.vsix
HOW TO GET STARTED
Download and install the SDK: http://roslyn.codeplex.com/
Run ”Install Roslyn Preview into Roslyn Experimental Hive.exe”
Install all the Visual Studio Extensions
Roslyn End User Preview.vsix
Roslyn SDK Project Templates.vsix
Roslyn Syntax Visualizer.vsix
Important to run
the .exe first and
then the vsix files
PROJECT TEMPLATES
PROJECT TEMPLATES
Console
applications
Create code
refactoring
plugins
Diagnostic,
build
extensions
SYNTAX VISUALIZER
DEMO
https://github.com/mastoj/RoslynSamples
KEY PARTS: CONSOLE APPLICATION
Install-package -pre Microsoft.CodeAnalysis.Csharp
// Create the syntax tree
var syntaxTree = CSharpSyntaxTree.ParseText(code);
// Compile the syntax tree
var compilation = CSharpCompilation.Create("Executable", new[] {syntaxTree},
_defaultReferences,
new CSharpCompilationOptions(OutputKind.ConsoleApplication));
private static IEnumerable<string> _defaultReferencesNames =
new [] { "mscorlib.dll", "System.dll", "System.Core.dll" };
private static string _assemblyPath =
Path.GetDirectoryName(typeof(object).Assembly.Location);
private static IEnumerable<MetadataFileReference> _defaultReferences =
_defaultReferencesNames.Select(
y => new MetadataFileReference(Path.Combine(_assemblyPath, y)));
KEY PARTS: CONSOLE APPLICATION
private Assembly CreateAssembly(CSharpCompilation compilation)
{
using(var outputStream = new MemoryStream())
using (var pdbStream = new MemoryStream())
{
// Emit assembly to streams
var result = compilation.Emit(outputStream, pdbStream: pdbStream);
if (!result.Success)
{
throw new CompilationException(result);
}
// Load the compiled assembly;
var assembly = Assembly.Load(outputStream.ToArray(), pdbStream.ToArray());
return assembly;
}
}
// Execute the assembly
assembly.EntryPoint.Invoke(null, new object[] { args });
KEY PARTS: DIAGNOSIS (DIAGNOSTIC ANALYZER)
Use project template: Diagnosis with Code Fix
public class DiagnosticAnalyzer : ISyntaxNodeAnalyzer<SyntaxKind>
public ImmutableArray<SyntaxKind> SyntaxKindsOfInterest
{
get { return ImmutableArray.Create(SyntaxKind.IfStatement); }
}
public void AnalyzeNode(SyntaxNode node, SemanticModel semanticModel,
Action<Diagnostic> addDiagnostic, CancellationToken cancellationToken)
What type analysis do
we want?
What kind of syntax
are we interested in?
Analyze and add
diagnostic.
KEY PARTS: DIAGNOSIS (CODE FIX PROVIDER)
public async Task<IEnumerable<CodeAction>> GetFixesAsync(Document document,
TextSpan span, IEnumerable<Diagnostic> diagnostics,
CancellationToken cancellationToken)
{
// Get the syntax root
var root = await document.GetSyntaxRootAsync(cancellationToken);
// Find the token that triggered the fix
var token = root.FindToken(span.Start);
if (token.IsKind(SyntaxKind.IfKeyword))
{
// Find the statement you want to change
var ifStatment = (IfStatementSyntax)token.Parent;
// Create replacement statement
var newIfStatement = ifStatment
.WithStatement(SyntaxFactory.Block(ifStatment.Statement))
.WithAdditionalAnnotations(Formatter.Annotation);
// New root based on the old one
var newRoot = root.ReplaceNode(ifStatment, newIfStatement);
return new[] {CodeAction.Create("Add braces",
document.WithSyntaxRoot(newRoot))};
}
return null;
}
EXISTING PROJECTS
ScriptCS
Build using the scripting engine in the CTP
Script engine is not available in the User Preview, but it will come back at some point
EXISTING PROJECTS
ScriptCS
Build using the scripting engine in the CTP
Script engine is not available in the User Preview, but it will come back at some point
Great for
exploration
SUMMARY
Roslyn is the new C# compiler from Microsoft
It’s open source
It’s a compiler-as-a-service
The have made easy to use project templates to extend Visual studio
RESOURCES
Good example blog post: http://tinyurl.com/RoslynExample
The Roslyn project: http://roslyn.codeplex.com/
Syntax Visualizer Overview: http://tinyurl.com/RoslynVisualizer
Source code to my demos: https://github.com/mastoj/RoslynSamples
Presentation from BUILD 2014: http://channel9.msdn.com/Events/Build/2014/2-577
My presentation on Slideshare: http://tinyurl.com/123Roslyn
Thank you!
@TomasJansson

Contenu connexe

Tendances

Understanding .Net Standards, .Net Core & .Net Framework
Understanding .Net Standards, .Net Core & .Net FrameworkUnderstanding .Net Standards, .Net Core & .Net Framework
Understanding .Net Standards, .Net Core & .Net Frameworkpunedevscom
 
Dot net platform and dotnet core fundamentals
Dot net platform and dotnet core fundamentalsDot net platform and dotnet core fundamentals
Dot net platform and dotnet core fundamentalsLalit Kale
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Languageintelliyole
 
Exploring Cloud Computing with Amazon Web Services (AWS)
Exploring Cloud Computing with Amazon Web Services (AWS)Exploring Cloud Computing with Amazon Web Services (AWS)
Exploring Cloud Computing with Amazon Web Services (AWS)Kalema Edgar
 
Continuous integration
Continuous integrationContinuous integration
Continuous integrationamscanne
 
Rancher 2.0 Technical Deep Dive
Rancher 2.0 Technical Deep DiveRancher 2.0 Technical Deep Dive
Rancher 2.0 Technical Deep DiveLINE Corporation
 
z/OS V2R2 Communications Server Overview
z/OS V2R2 Communications Server Overviewz/OS V2R2 Communications Server Overview
z/OS V2R2 Communications Server OverviewzOSCommserver
 
Kubernetes Networking 101
Kubernetes Networking 101Kubernetes Networking 101
Kubernetes Networking 101Weaveworks
 
Cognitive Computing in IBM Spectrum LSF
Cognitive Computing in IBM Spectrum LSFCognitive Computing in IBM Spectrum LSF
Cognitive Computing in IBM Spectrum LSFGabor Samu
 
IT Infrastructure Automation with Ansible
IT Infrastructure Automation with AnsibleIT Infrastructure Automation with Ansible
IT Infrastructure Automation with AnsibleDio Pratama
 
Introduction To Dotnet
Introduction To DotnetIntroduction To Dotnet
Introduction To DotnetSAMIR BHOGAYTA
 
Introduction to Spark Streaming & Apache Kafka | Big Data Hadoop Spark Tutori...
Introduction to Spark Streaming & Apache Kafka | Big Data Hadoop Spark Tutori...Introduction to Spark Streaming & Apache Kafka | Big Data Hadoop Spark Tutori...
Introduction to Spark Streaming & Apache Kafka | Big Data Hadoop Spark Tutori...CloudxLab
 

Tendances (20)

Understanding .Net Standards, .Net Core & .Net Framework
Understanding .Net Standards, .Net Core & .Net FrameworkUnderstanding .Net Standards, .Net Core & .Net Framework
Understanding .Net Standards, .Net Core & .Net Framework
 
Dot net platform and dotnet core fundamentals
Dot net platform and dotnet core fundamentalsDot net platform and dotnet core fundamentals
Dot net platform and dotnet core fundamentals
 
Terraform
TerraformTerraform
Terraform
 
The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Language
 
Exploring Cloud Computing with Amazon Web Services (AWS)
Exploring Cloud Computing with Amazon Web Services (AWS)Exploring Cloud Computing with Amazon Web Services (AWS)
Exploring Cloud Computing with Amazon Web Services (AWS)
 
Jenkins presentation
Jenkins presentationJenkins presentation
Jenkins presentation
 
Microservizi & DevOps
Microservizi & DevOpsMicroservizi & DevOps
Microservizi & DevOps
 
Continuous integration
Continuous integrationContinuous integration
Continuous integration
 
Rancher 2.0 Technical Deep Dive
Rancher 2.0 Technical Deep DiveRancher 2.0 Technical Deep Dive
Rancher 2.0 Technical Deep Dive
 
DevOps with Kubernetes
DevOps with KubernetesDevOps with Kubernetes
DevOps with Kubernetes
 
Apache tomcat
Apache tomcatApache tomcat
Apache tomcat
 
Kubernetes networking & Security
Kubernetes networking & SecurityKubernetes networking & Security
Kubernetes networking & Security
 
z/OS V2R2 Communications Server Overview
z/OS V2R2 Communications Server Overviewz/OS V2R2 Communications Server Overview
z/OS V2R2 Communications Server Overview
 
Kubernetes Networking 101
Kubernetes Networking 101Kubernetes Networking 101
Kubernetes Networking 101
 
Cognitive Computing in IBM Spectrum LSF
Cognitive Computing in IBM Spectrum LSFCognitive Computing in IBM Spectrum LSF
Cognitive Computing in IBM Spectrum LSF
 
IT Infrastructure Automation with Ansible
IT Infrastructure Automation with AnsibleIT Infrastructure Automation with Ansible
IT Infrastructure Automation with Ansible
 
Yaml
YamlYaml
Yaml
 
Introduction To Dotnet
Introduction To DotnetIntroduction To Dotnet
Introduction To Dotnet
 
Jenkins-CI
Jenkins-CIJenkins-CI
Jenkins-CI
 
Introduction to Spark Streaming & Apache Kafka | Big Data Hadoop Spark Tutori...
Introduction to Spark Streaming & Apache Kafka | Big Data Hadoop Spark Tutori...Introduction to Spark Streaming & Apache Kafka | Big Data Hadoop Spark Tutori...
Introduction to Spark Streaming & Apache Kafka | Big Data Hadoop Spark Tutori...
 

Similaire à Roslyn

01 Introduction to programming
01 Introduction to programming01 Introduction to programming
01 Introduction to programmingmaznabili
 
Parse cloud code
Parse cloud codeParse cloud code
Parse cloud code維佋 唐
 
Introduction to Roslyn and its use in program development
Introduction to Roslyn and its use in program developmentIntroduction to Roslyn and its use in program development
Introduction to Roslyn and its use in program developmentEkaterina Milovidova
 
Introduction to Roslyn and its use in program development
Introduction to Roslyn and its use in program developmentIntroduction to Roslyn and its use in program development
Introduction to Roslyn and its use in program developmentPVS-Studio
 
JCConf 2016 - Dataflow Workshop Labs
JCConf 2016 - Dataflow Workshop LabsJCConf 2016 - Dataflow Workshop Labs
JCConf 2016 - Dataflow Workshop LabsSimon Su
 
Cocoapods and Most common used library in Swift
Cocoapods and Most common used library in SwiftCocoapods and Most common used library in Swift
Cocoapods and Most common used library in SwiftWan Muzaffar Wan Hashim
 
Salt and pepper — native code in the browser Browser using Google native Client
Salt and pepper — native code in the browser Browser using Google native ClientSalt and pepper — native code in the browser Browser using Google native Client
Salt and pepper — native code in the browser Browser using Google native ClientMayflower GmbH
 
New Year PVS-Studio 6.00 Release: Scanning Roslyn
New Year PVS-Studio 6.00 Release: Scanning RoslynNew Year PVS-Studio 6.00 Release: Scanning Roslyn
New Year PVS-Studio 6.00 Release: Scanning RoslynPVS-Studio
 
The First C# Project Analyzed
The First C# Project AnalyzedThe First C# Project Analyzed
The First C# Project AnalyzedPVS-Studio
 
QA Fest 2018. Adam Stasiak. React Native is Coming – the story of hybrid mobi...
QA Fest 2018. Adam Stasiak. React Native is Coming – the story of hybrid mobi...QA Fest 2018. Adam Stasiak. React Native is Coming – the story of hybrid mobi...
QA Fest 2018. Adam Stasiak. React Native is Coming – the story of hybrid mobi...QAFest
 
(1) c sharp introduction_basics_dot_net
(1) c sharp introduction_basics_dot_net(1) c sharp introduction_basics_dot_net
(1) c sharp introduction_basics_dot_netNico Ludwig
 
AWS CodeDeploy - basic intro
AWS CodeDeploy - basic introAWS CodeDeploy - basic intro
AWS CodeDeploy - basic introAnton Babenko
 
AWS Webcast - Build Agile Applications in AWS Cloud for Government
AWS Webcast - Build Agile Applications in AWS Cloud for GovernmentAWS Webcast - Build Agile Applications in AWS Cloud for Government
AWS Webcast - Build Agile Applications in AWS Cloud for GovernmentAmazon Web Services
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLsintelliyole
 
Open Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVMOpen Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVMTom Lee
 
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014Susan Potter
 
"The OpenCV Open Source Computer Vision Library: Latest Developments," a Pres...
"The OpenCV Open Source Computer Vision Library: Latest Developments," a Pres..."The OpenCV Open Source Computer Vision Library: Latest Developments," a Pres...
"The OpenCV Open Source Computer Vision Library: Latest Developments," a Pres...Edge AI and Vision Alliance
 
Playing with SIMBL - Mobile Jazz Inspirational Talks
Playing with SIMBL - Mobile Jazz Inspirational TalksPlaying with SIMBL - Mobile Jazz Inspirational Talks
Playing with SIMBL - Mobile Jazz Inspirational TalksMobile Jazz
 

Similaire à Roslyn (20)

01 Introduction to programming
01 Introduction to programming01 Introduction to programming
01 Introduction to programming
 
Parse cloud code
Parse cloud codeParse cloud code
Parse cloud code
 
Introduction to Roslyn and its use in program development
Introduction to Roslyn and its use in program developmentIntroduction to Roslyn and its use in program development
Introduction to Roslyn and its use in program development
 
Introduction to Roslyn and its use in program development
Introduction to Roslyn and its use in program developmentIntroduction to Roslyn and its use in program development
Introduction to Roslyn and its use in program development
 
Introduction to Programming Lesson 01
Introduction to Programming Lesson 01Introduction to Programming Lesson 01
Introduction to Programming Lesson 01
 
JCConf 2016 - Dataflow Workshop Labs
JCConf 2016 - Dataflow Workshop LabsJCConf 2016 - Dataflow Workshop Labs
JCConf 2016 - Dataflow Workshop Labs
 
Cocoapods and Most common used library in Swift
Cocoapods and Most common used library in SwiftCocoapods and Most common used library in Swift
Cocoapods and Most common used library in Swift
 
Salt and pepper — native code in the browser Browser using Google native Client
Salt and pepper — native code in the browser Browser using Google native ClientSalt and pepper — native code in the browser Browser using Google native Client
Salt and pepper — native code in the browser Browser using Google native Client
 
New Year PVS-Studio 6.00 Release: Scanning Roslyn
New Year PVS-Studio 6.00 Release: Scanning RoslynNew Year PVS-Studio 6.00 Release: Scanning Roslyn
New Year PVS-Studio 6.00 Release: Scanning Roslyn
 
The First C# Project Analyzed
The First C# Project AnalyzedThe First C# Project Analyzed
The First C# Project Analyzed
 
It's always your fault
It's always your faultIt's always your fault
It's always your fault
 
QA Fest 2018. Adam Stasiak. React Native is Coming – the story of hybrid mobi...
QA Fest 2018. Adam Stasiak. React Native is Coming – the story of hybrid mobi...QA Fest 2018. Adam Stasiak. React Native is Coming – the story of hybrid mobi...
QA Fest 2018. Adam Stasiak. React Native is Coming – the story of hybrid mobi...
 
(1) c sharp introduction_basics_dot_net
(1) c sharp introduction_basics_dot_net(1) c sharp introduction_basics_dot_net
(1) c sharp introduction_basics_dot_net
 
AWS CodeDeploy - basic intro
AWS CodeDeploy - basic introAWS CodeDeploy - basic intro
AWS CodeDeploy - basic intro
 
AWS Webcast - Build Agile Applications in AWS Cloud for Government
AWS Webcast - Build Agile Applications in AWS Cloud for GovernmentAWS Webcast - Build Agile Applications in AWS Cloud for Government
AWS Webcast - Build Agile Applications in AWS Cloud for Government
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
 
Open Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVMOpen Source Compiler Construction for the JVM
Open Source Compiler Construction for the JVM
 
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
 
"The OpenCV Open Source Computer Vision Library: Latest Developments," a Pres...
"The OpenCV Open Source Computer Vision Library: Latest Developments," a Pres..."The OpenCV Open Source Computer Vision Library: Latest Developments," a Pres...
"The OpenCV Open Source Computer Vision Library: Latest Developments," a Pres...
 
Playing with SIMBL - Mobile Jazz Inspirational Talks
Playing with SIMBL - Mobile Jazz Inspirational TalksPlaying with SIMBL - Mobile Jazz Inspirational Talks
Playing with SIMBL - Mobile Jazz Inspirational Talks
 

Plus de Tomas Jansson

Functional webapplicaations using fsharp and suave
Functional webapplicaations using fsharp and suaveFunctional webapplicaations using fsharp and suave
Functional webapplicaations using fsharp and suaveTomas Jansson
 
F# as our day job by 2016
F# as our day job by 2016F# as our day job by 2016
F# as our day job by 2016Tomas Jansson
 
What does the future hold for us in asp.net 5
What does the future hold for us in asp.net 5What does the future hold for us in asp.net 5
What does the future hold for us in asp.net 5Tomas Jansson
 
OWIN Web API with Linky
OWIN Web API with LinkyOWIN Web API with Linky
OWIN Web API with LinkyTomas Jansson
 
File -> new project to deploy in 10 minutes with TeamCity and Octopus Deploy
File -> new project to deploy in 10 minutes with TeamCity and Octopus DeployFile -> new project to deploy in 10 minutes with TeamCity and Octopus Deploy
File -> new project to deploy in 10 minutes with TeamCity and Octopus DeployTomas Jansson
 
Getting started with Elasticsearch and .NET
Getting started with Elasticsearch and .NETGetting started with Elasticsearch and .NET
Getting started with Elasticsearch and .NETTomas Jansson
 
Deployment taken seriously with Octopus Deploy and TeamCity
Deployment taken seriously with Octopus Deploy and TeamCityDeployment taken seriously with Octopus Deploy and TeamCity
Deployment taken seriously with Octopus Deploy and TeamCityTomas Jansson
 
NServiceBus workshop presentation
NServiceBus workshop presentationNServiceBus workshop presentation
NServiceBus workshop presentationTomas Jansson
 
SignalR - Building an async web app with .NET
SignalR - Building an async web app with .NETSignalR - Building an async web app with .NET
SignalR - Building an async web app with .NETTomas Jansson
 
REST for .NET - Introduction to ASP.NET Web API
REST for .NET - Introduction to ASP.NET Web APIREST for .NET - Introduction to ASP.NET Web API
REST for .NET - Introduction to ASP.NET Web APITomas Jansson
 

Plus de Tomas Jansson (12)

Functional webapplicaations using fsharp and suave
Functional webapplicaations using fsharp and suaveFunctional webapplicaations using fsharp and suave
Functional webapplicaations using fsharp and suave
 
F# as our day job by 2016
F# as our day job by 2016F# as our day job by 2016
F# as our day job by 2016
 
What does the future hold for us in asp.net 5
What does the future hold for us in asp.net 5What does the future hold for us in asp.net 5
What does the future hold for us in asp.net 5
 
Polyglot heaven
Polyglot heavenPolyglot heaven
Polyglot heaven
 
OWIN Web API with Linky
OWIN Web API with LinkyOWIN Web API with Linky
OWIN Web API with Linky
 
File -> new project to deploy in 10 minutes with TeamCity and Octopus Deploy
File -> new project to deploy in 10 minutes with TeamCity and Octopus DeployFile -> new project to deploy in 10 minutes with TeamCity and Octopus Deploy
File -> new project to deploy in 10 minutes with TeamCity and Octopus Deploy
 
Getting started with Elasticsearch and .NET
Getting started with Elasticsearch and .NETGetting started with Elasticsearch and .NET
Getting started with Elasticsearch and .NET
 
Deployment taken seriously with Octopus Deploy and TeamCity
Deployment taken seriously with Octopus Deploy and TeamCityDeployment taken seriously with Octopus Deploy and TeamCity
Deployment taken seriously with Octopus Deploy and TeamCity
 
State or intent
State or intentState or intent
State or intent
 
NServiceBus workshop presentation
NServiceBus workshop presentationNServiceBus workshop presentation
NServiceBus workshop presentation
 
SignalR - Building an async web app with .NET
SignalR - Building an async web app with .NETSignalR - Building an async web app with .NET
SignalR - Building an async web app with .NET
 
REST for .NET - Introduction to ASP.NET Web API
REST for .NET - Introduction to ASP.NET Web APIREST for .NET - Introduction to ASP.NET Web API
REST for .NET - Introduction to ASP.NET Web API
 

Dernier

Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 

Dernier (20)

Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 

Roslyn

  • 1. ROSLYN What it is and how to get started Oslo/NNUG Tomas Jansson 27/05/2014
  • 2. THIS IS ME Tomas Jansson Manager & Practice Lead .NET BEKK Oslo @TomasJansson tomas.jansson@bekk.no github.com/mastoj blog.tomasjansson.com
  • 3. AGENDA What is Roslyn Summary The compile pipeline How to get started Existing projectsDemo
  • 4. WHAT IS ROSLYN? The .NET Compiler Platform ("Roslyn") provides open-source C# and Visual Basic compilers with rich code analysis APIs. You can build code analysis tools with the same APIs that Microsoft is using to implement Visual Studio! http://roslyn.codeplex.com/
  • 5. WHAT IS ROSLYN? The .NET Compiler Platform ("Roslyn") provides open-source C# and Visual Basic compilers with rich code analysis APIs. You can build code analysis tools with the same APIs that Microsoft is using to implement Visual Studio! http://roslyn.codeplex.com/
  • 6. WHAT IS ROSLYN? It’s a ”compiler-as-a-service (caas)”
  • 7. WHAT IS A COMPILER-AS-A-SERVICE? ”C# compiler is like a box of chocolates, you never know what you’re gonna get” http://theawesomedaily.theawesomedaily.netdna-cdn.com/wp-content/uploads/2013/12/forrest-gump-original.jpeg
  • 8. WHAT IS A COMPILER-AS-A-SERVICE? ”C# compiler is like a box of chocolates, you never know what you’re gonna get” This was life before Roslyn http://theawesomedaily.theawesomedaily.netdna-cdn.com/wp-content/uploads/2013/12/forrest-gump-original.jpeg
  • 9. Now we know exactly what we gonna get!
  • 10. WHY? Power to the developers Easier to add features (for Microsoft)
  • 12. HOW TO GET STARTED Download and install the SDK: http://roslyn.codeplex.com/ Run ”Install Roslyn Preview into Roslyn Experimental Hive.exe” Install all the Visual Studio Extensions Roslyn End User Preview.vsix Roslyn SDK Project Templates.vsix Roslyn Syntax Visualizer.vsix
  • 13. HOW TO GET STARTED Download and install the SDK: http://roslyn.codeplex.com/ Run ”Install Roslyn Preview into Roslyn Experimental Hive.exe” Install all the Visual Studio Extensions Roslyn End User Preview.vsix Roslyn SDK Project Templates.vsix Roslyn Syntax Visualizer.vsix Important to run the .exe first and then the vsix files
  • 18. KEY PARTS: CONSOLE APPLICATION Install-package -pre Microsoft.CodeAnalysis.Csharp // Create the syntax tree var syntaxTree = CSharpSyntaxTree.ParseText(code); // Compile the syntax tree var compilation = CSharpCompilation.Create("Executable", new[] {syntaxTree}, _defaultReferences, new CSharpCompilationOptions(OutputKind.ConsoleApplication)); private static IEnumerable<string> _defaultReferencesNames = new [] { "mscorlib.dll", "System.dll", "System.Core.dll" }; private static string _assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location); private static IEnumerable<MetadataFileReference> _defaultReferences = _defaultReferencesNames.Select( y => new MetadataFileReference(Path.Combine(_assemblyPath, y)));
  • 19. KEY PARTS: CONSOLE APPLICATION private Assembly CreateAssembly(CSharpCompilation compilation) { using(var outputStream = new MemoryStream()) using (var pdbStream = new MemoryStream()) { // Emit assembly to streams var result = compilation.Emit(outputStream, pdbStream: pdbStream); if (!result.Success) { throw new CompilationException(result); } // Load the compiled assembly; var assembly = Assembly.Load(outputStream.ToArray(), pdbStream.ToArray()); return assembly; } } // Execute the assembly assembly.EntryPoint.Invoke(null, new object[] { args });
  • 20. KEY PARTS: DIAGNOSIS (DIAGNOSTIC ANALYZER) Use project template: Diagnosis with Code Fix public class DiagnosticAnalyzer : ISyntaxNodeAnalyzer<SyntaxKind> public ImmutableArray<SyntaxKind> SyntaxKindsOfInterest { get { return ImmutableArray.Create(SyntaxKind.IfStatement); } } public void AnalyzeNode(SyntaxNode node, SemanticModel semanticModel, Action<Diagnostic> addDiagnostic, CancellationToken cancellationToken) What type analysis do we want? What kind of syntax are we interested in? Analyze and add diagnostic.
  • 21. KEY PARTS: DIAGNOSIS (CODE FIX PROVIDER) public async Task<IEnumerable<CodeAction>> GetFixesAsync(Document document, TextSpan span, IEnumerable<Diagnostic> diagnostics, CancellationToken cancellationToken) { // Get the syntax root var root = await document.GetSyntaxRootAsync(cancellationToken); // Find the token that triggered the fix var token = root.FindToken(span.Start); if (token.IsKind(SyntaxKind.IfKeyword)) { // Find the statement you want to change var ifStatment = (IfStatementSyntax)token.Parent; // Create replacement statement var newIfStatement = ifStatment .WithStatement(SyntaxFactory.Block(ifStatment.Statement)) .WithAdditionalAnnotations(Formatter.Annotation); // New root based on the old one var newRoot = root.ReplaceNode(ifStatment, newIfStatement); return new[] {CodeAction.Create("Add braces", document.WithSyntaxRoot(newRoot))}; } return null; }
  • 22. EXISTING PROJECTS ScriptCS Build using the scripting engine in the CTP Script engine is not available in the User Preview, but it will come back at some point
  • 23. EXISTING PROJECTS ScriptCS Build using the scripting engine in the CTP Script engine is not available in the User Preview, but it will come back at some point Great for exploration
  • 24. SUMMARY Roslyn is the new C# compiler from Microsoft It’s open source It’s a compiler-as-a-service The have made easy to use project templates to extend Visual studio
  • 25. RESOURCES Good example blog post: http://tinyurl.com/RoslynExample The Roslyn project: http://roslyn.codeplex.com/ Syntax Visualizer Overview: http://tinyurl.com/RoslynVisualizer Source code to my demos: https://github.com/mastoj/RoslynSamples Presentation from BUILD 2014: http://channel9.msdn.com/Events/Build/2014/2-577 My presentation on Slideshare: http://tinyurl.com/123Roslyn

Notes de l'éditeur

  1. Present the topic
  2. Keep it short
  3. Each phase is a separate component. * Parsing phase – tokenized and parsed (Syntax tree) * Declaration phase – forms the symbols (hierarchical symbol table) * Binding phase – match code against the symbols (model of the result from the compiler’s semantic analysis) * Emit phase – emitted as an assembly, IL code We don’t need to know all the details to get started, just do it