- What Is Roslyn
- Visual Studio Roslyn Park
- Visual Studio Roslyn
- Roslyn Cybersecurity Code Analyzers
- Visual Studio Roslyn Pa
Scripting support in the .NET Compiler Platform (formerly known as Roslyn) has been a long time coming. It was originally introduced more than a year ago and then removed while the team considered what the ideal API should look like. It was recently reintroduced into the master source branch on GitHub, though as of this blog post it still isn't available on the nightly MyGet feed or on NuGet. In this post I will explain how to obtain and build the new scripting bits (including on a system without Visual Studio 2015 - it can actually be built using only Visual Studio 2013), introduce some of the scripting functionality, and show some scenarios where this might be helpful in your own applications. I also want to caveat this post by saying that it may go out of date quickly. The .NET Compiler Platform is under heavy development and it is changing frequently, including the public API. While I wouldn't expect any sweeping changes in the scripting support at this point, many of the details are subject to change.
Back in 2011, I noted that Roslyn would be a post-Visual Studio 2012 deliverable. It turned out it also is a post-Visual Studio 2013 one, as well. Microsoft's last generally-available preview of.
- Roslyn is definitely important and a good step forward for Microsoft in that it should help Visual Studio users take advantage of more C# and VB.NET code editing and analysis features in Visual Studio out of the box.
- The.NET Compiler Platform Roslyn is the open-source implementation of both the C# and Visual Basic compilers with an API surface for building code analysis tools. C# and Visual Basic Language Feature Suggestions If you want to suggest a new feature for the C# or Visual Basic languages go here.
Obtaining and Building
Since the scripting support isn't available in one of the binary distribution channels like the nightly MyGet feed or on NuGet, you'll need to obtain and build the .NET Compiler Platform from source in order to use them. Luckily the development team has been working hard to ensure this part is straightforward. The first step is to clone the repository from GitHub using the following command (or your favorite Git GUI): git clone https://github.com/dotnet/roslyn.git
.
The readme file says that to build the libraries you'll need Visual Studio 2015, but that's not actually true. You can build the .NET Compiler Platform just fine with only Visual Studio 2013 using the special Roslyn2013.sln
solution file they've provided. In fact, it's just a simple two-command process. Make sure to run these commands from a Visual Studio command prompt. First you have to restore the NuGet packages and then build the solution. You might get some warnings about missing FxCop, but those are safe to ignore. From the src
directory in the repository run:
This will compile a bunch of libraries to the BinariesDebug
directory. You don't need all of them for scripting. Assuming you want to script in C#, the libraries you need are:
- Microsoft.CodeAnalysis
- Microsoft.CodeAnalysis.CSharp
- Microsoft.CodeAnalysis.Desktop
- Microsoft.CodeAnalysis.CSharp.Desktop
- Microsoft.CodeAnalysis.Scripting
- Microsoft.CodeAnalysis.Scripting.CSharp
- System.Collections.Immutable
- System.Reflection.Metadata
I also ran into a problem with VBCSCompiler.exe
crashing during the build, which appears like it might be related to this issue. In any case, upgrading to the latest .NET 4.5.2 runtime resolved my problem. Of course, if you have Visual Studio 2015 installed you can also try building the other Roslyn.sln
solution.
Your First Script
Writing a simple script in the .NET Compiler Platform is really easy. For the remainder of this post I will discuss scripting in C#. There is a nearly identical API for scripting in Visual Basic if that's your thing. The CSharpScript
class has a bunch of static methods that provide a good entry point to the API. Perhaps the most straightforward of these is CSharpScript.Eval()
which will evaluate C# statements and return a result. For example:
This returns an int
with a value of 3
. See, easy right? If you want more control, there's also CSharpScript.Create()
which returns a CSharpScript
object suitable for further manipulation before evaluation and CSharpScript.Run()
which evaluates the script and returns a ScriptState
object with the return value and other state information useful for REPL scenarios.
Getting Variables
As you saw above, it's easy to get the return value from the script using the CSharpScript.Eval()
method. But what about other variables that get created during evaluation? We can get those as well by using the ScriptState
object you get back from calling CSharpScript.Run()
. It contains a member called Variables
(of type ScriptVariables
) that enumerates ScriptVariable
objects with the name, type, and value for each variable the script created. For example:
What Is Roslyn
References and Namespaces
If you want to do anything reasonably advanced, you'll probably need to include references and import namespaces for additional assemblies. Thankfully this is also really easy. There are a number of ways to do it, but the easiest is to use a ScriptOptions
object which is accepted by any of the three CSharpScript
static methods. The default ScriptOptions
includes the System
assembly and namespace and will search for additional assemblies in the current runtime directory. To modify this, start with ScriptOptions.Default
and use it's fluent interface to setup additional references and namespaces (you can also create your own ScriptOptions
if you don't want the default System
assembly and namespace). Use ScriptOptions.AddReferences()
to add references and ScriptOptions.AddNamespaces()
to add namespaces (there are also several variations on these methods). ScriptOptions.AddReferences()
accepts a variety of different ways of referring to assemblies, including the .NET Compiler Platform type MetadataReference
if you're used to using that from the other portions of the platform. Here is an example of including System.IO
support in a script:
Dynamic Support
Getting support for dynamic
in your script can be a challenge if only because it's not obvious which assemblies need to be referenced to support it. The answer is that you need System.Core
and Microsoft.CSharp
. That's all that's strictly needed, but if you also want support for ExpandoObject
you'll need an extra reference and namespace support for System.Dynamic
. Here is an example script with dynamic
support (note that there's no magic to the types I pass to Assembly.GetAssembly()
; these just happen to be types I know are defined in the required assemblies). Note also that I had to use CSharpScript.Run()
since you can't directly return values from a script so I had to store my value in a variable and get it from the ScriptState
object as we saw earlier.
Setting Globals
We've seen a number of ways of getting data out of the script, but what about getting data into the script? This is one of my favorite features of the scripting API because it's so easy to use. To set the data available to the script, you just have to pass an arbitrary object to one of the three CSharpScript
static methods. The members of this object will then be available to your script as globals. For example:
Note that the scripting engine respects protection levels so it's not possible to directly pass an anonymous object to the script because anonymous objects are usually scoped to the method in which they appear.
Creating a Delegate
Finally you may want to compile the script, but store it for later reuse. Thankfully, there is also an easy way to create a delegate from any method in your script by calling ScriptState.CreateDelegate()
.
Conclusions
By now you've hopefully thought of some use cases for the new scripting capability. My personal favorite at the moment is using this to drive configuration files. Instead of configuring your application using XML or JSON, why not set up a scripting environment and let your users (or you) write code to configure the application. If this interests you, I should also mention ConfigR which does exactly this while abstracting away many of the details. Any post on C# scripting would also be incomplete without a mention of scriptcs which provides a REPL for you to use on the command line for executing your own script files.
There's also a lot more to the .NET Compiler Platform scripting support than what I've discussed here. It's even possible to compile your script to a syntax tree and then use the rest of the .NET Compiler Platform capabilities to explore, analyze, and manipulate the script. A good place to continue exploring is the enhanced Roslyn source view site.
Please enable JavaScript to view the comments powered by Disqus.comments powered by DisqusVisual Studio Roslyn Park
Original author(s) | Microsoft |
---|---|
Developer(s) | .NET Foundation |
Stable release | Visual-Studio-2019-Version-16.7.3 / September 4, 2020; 7 months ago |
Repository | github.com/dotnet/roslyn |
Written in | C#, Visual Basic .NET |
Operating system | Microsoft Windows, Linux |
Platform | IA-32, x86-64 |
Type | Compiler |
License | MIT License |
Website | docs.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/ |
.NET Compiler Platform, also known by its nickname Roslyn,[1] is a set of open-sourcecompilers and code analysis APIs for C# and Visual Basic .NET languages from Microsoft.[2]
The project notably includes self-hosting versions of the C# and VB.NET compilers – compilers written in the languages themselves. The compilers are available via the traditional command-line programs but also as APIs available natively from within .NET code. Roslyn exposes modules for syntactic (lexical) analysis of code, semantic analysis, dynamic compilation to CIL, and code emission.[3]
Features[edit]
The most notable primary features of Roslyn include:[citation needed]
- Compilers for the C# and Visual Basic .NET languages exposed as services via APIs.
- APIs for code analysis and refactoring.
History[edit]
The code name 'Roslyn' was first written by Eric Lippert (a former Microsoft engineer[4]) in a post[5] that he published to hire developers for a new project. He first said that the origin of the name was because of Roslyn, Washington, but later in the post he speaks ironically about the 'northern exposure' of its office. The city of Roslyn was one of the places where the television series Northern Exposure was filmed.[6]
Microsoft made a community technology preview (CTP) available for public download in October 2011. It installed as an extension to Visual Studio 2010 SP1.[7]
The CTP was updated on September 2012[8] to include many updates to the Roslyn APIs introduced in the June 2012 and October 2011 CTPs, including breaking changes.[9] While the June 2012 CTP API is complete for the compilers, not all features were implemented for the C# and VB.NET languages.[10]
At the Build 2014 conference in San Francisco April 2014, Microsoft made the 'Roslyn' project open-source and released a preview of the language integration for Visual Studio 2013. As of April 3, 2014, Roslyn is under the Apache License 2.0.[2] The project was effectively transferred under the stewardship of the newly founded .NET Foundation.[11] At the same conference, Xamarin announced that they are working on integrating the new compilers and tools in Xamarin Studio.[citation needed]
Visual Studio Roslyn
The compilers were not feature-complete in this release. Each of the compilers contains features that are planned for the coming language versions (C# 6 and Visual Basic.NET 14). The APIs are also available through the NuGet package manager.[citation needed]
Roslyn's first RTM release was with Visual Studio 2015.[12] Roslyn currently only supports VB and C#, and the compilers were written in their respective languages.[13]
In January 2015, Microsoft moved the Roslyn source code from CodePlex to GitHub.[14]
Architecture[edit]
Traditionally compilers have been a black box for application developers[dubious]. With increasing complexity and demands for source code analysis in modern integrated development environments, however, compilers need to expose application programming interfaces (APIs) that will help developers to directly perform phases of compilation such as lexical and syntactic structure analysis of source code. Roslyn was designed with that intent from the beginning. This reduces the barrier in developing tools specifically designed for source code analysis. APIs of Roslyn are of three types: feature APIs, work-space APIs and compiler APIs. Feature APIs allow source code tool developers to do code refactoring and fixes. Work-space APIs allow plugin developers to perform actions specifically required in integrated development environments (IDEs) like Visual Studio such as finding references of a variable or code formatting. Compiler APIs allow even more sophisticated analysis of source code, by exposing direct calls to perform syntax tree and binding flow analysis.[15] Using an open-source implementation of Common Language Infrastructure (CLI) such as .NET Core, Roslyn will be able to compile in a platform-agnostic manner capable of running CLI code in Linux, OS X, and Windows.[citation needed]/
See also[edit]
Roslyn Cybersecurity Code Analyzers
References[edit]
- ^'C# and Visual Basic - Use Roslyn to Write a Live Code Analyzer for Your API'. msdn.microsoft.com. Retrieved January 7, 2019.
- ^ ab.NET Compiler Platform ('Roslyn') on GitHub
- ^Neil McAllister, Microsoft's Roslyn: Reinventing the compiler as we know it, DEVELOPER_WORLD, 2011-10-20
- ^'Fabulous adventures in coding'. About Eric Lippert. Eric Lippert.
- ^'Hiring for Roslyn'. Eric Lippert's MSDN blog. Eric Lippert. December 16, 2010.
- ^Muir, Pat (October 5, 2014). 'Roslyn hopes new TV show brings 15 more minutes of fame'. Yakima Herald. Archived from the original on November 2, 2014. Retrieved November 1, 2014.
- ^Microsoft 'Roslyn' CTPArchived April 18, 2012, at the Wayback Machine, Microsoft Download Center
- ^Microsoft 'Roslyn' CTP, Microsoft Download Center
- ^What's New in the Microsoft 'Roslyn' September 2012 CTP, Visual Studio vNext Forums
- ^Known Limitations and Unimplemented Language Features, Visual Studio vNext Forums
- ^.NET Foundation – Open Source Foundation for the .NET Community
- ^Visual Studio 2015 RTM, 2015-07-20
- ^Microsoft Roslyn vs. CodeDom
- ^We're moving to GitHub!, MSDN VBTeam Blog, 2015-01-10
- ^Overview of Roslyn from GitHub documentation
External links[edit]
Visual Studio Roslyn Pa
Wikibooks has a book on the topic of: .NET Development Foundation |
- Official website
- Introducing the Microsoft “Roslyn” CTP on the C# Frequently Asked Questions MSDN blog
- Throwing the Big Switch on Roslyn on the C# Frequently Asked Questions MSDN blog