SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Spring Boot
Spring BootSpring Boot
Spring Boot
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
 
Fast C++ Web Servers
Fast C++ Web ServersFast C++ Web Servers
Fast C++ Web Servers
 
Docker for .NET Developers
Docker for .NET DevelopersDocker for .NET Developers
Docker for .NET Developers
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
C# Basics
C# BasicsC# Basics
C# Basics
 
Java Concurrency by Example
Java Concurrency by ExampleJava Concurrency by Example
Java Concurrency by Example
 
Java 9 Features
Java 9 FeaturesJava 9 Features
Java 9 Features
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Introduction to GNU Make Programming Language
Introduction to GNU Make Programming LanguageIntroduction to GNU Make Programming Language
Introduction to GNU Make Programming Language
 
C# Method overloading
C# Method overloadingC# Method overloading
C# Method overloading
 
Docker compose
Docker composeDocker compose
Docker compose
 
Drools rule Concepts
Drools rule ConceptsDrools rule Concepts
Drools rule Concepts
 
Common language runtime clr
Common language runtime clrCommon language runtime clr
Common language runtime clr
 
Java Class Loader
Java Class LoaderJava Class Loader
Java Class Loader
 
Read-only rootfs: theory and practice
Read-only rootfs: theory and practiceRead-only rootfs: theory and practice
Read-only rootfs: theory and practice
 
Gradle Introduction
Gradle IntroductionGradle Introduction
Gradle Introduction
 
Gradle
GradleGradle
Gradle
 
Packages,interfaces and exceptions
Packages,interfaces and exceptionsPackages,interfaces and exceptions
Packages,interfaces and exceptions
 
TypeScript Best Practices
TypeScript Best PracticesTypeScript Best Practices
TypeScript Best Practices
 

Similar a 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
 

Similar a 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
 

Más 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
 

Más 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
 

Último

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 

Último (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 

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

Notas del editor

  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