Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable C# scripting in VS 2015?

How do I configure VS 2015 to enable Roslyn's C# scripting capabilities?

I've tried installing various Nuget packages, including both the 1.0 and 1.1.0-beta1 versions of Microsoft.CodeAnalysis.CSharp, Microsoft.CodeAnalysis.Scripting.CSharp, etc., but I can't get any of the examples I've found online to work. I am getting "type not found" errors, i.e.,

var scriptEngine = new ScriptEngine();

... fails because the type "ScriptEngine" is not found.

Can someone provide as recipe that includes which nuget packages to install, which using statements are required, etc., to implement Roslyn scripting?

UPDATE #1:

I have made some progress, but still having issues. I get a bunch of compiler warnings and then a TypeInitilizationException which is apparently due to a component version mismatch.

I'm now using the following example code (taken from a test), and there's no missing types:

using System;  
using Microsoft.CodeAnalysis.Scripting.CSharp;  

namespace RoslynScriptingTest {  
   class Program {  
      static void Main(string[] args) {  
         var script = CSharpScript.Create("1 + 2");  
         var fn = script.CreateDelegate();  
         var value = fn();  
         Console.WriteLine("value={0}", value.ToString());  
      }  
   }  
}  

I've loaded all of the nightly packages that are available at https://www.myget.org/F/roslyn-nightly/.

I get a series of build warnings that refer to Microsoft.CodeAnalysis, v1.1.0.0.

Running the exe despite the warnings yields the TypeInitilizationException mentioned above. Based on the stacktrace, the TypeInitializationError is caused by a version mismatch for System.Reflection.Metadata.dll.

I am not sure where to go from here. I don't understand how the scripting-related packages/components fit together. I saw some posts from earlier this year that describe building Roslyn completely. I have not done that. Is that necessary?

This is reminding me of DLL hell from the old days.

like image 452
Johnny Cee Avatar asked Jul 26 '15 02:07

Johnny Cee


2 Answers

The scripting APIs are still in progress, and were removed from the release packages.

Try the nightlies instead.

like image 122
SLaks Avatar answered Sep 30 '22 12:09

SLaks


With visual studio 2015 update1 the REPL is back and scripting api's are enabled.

Here is what Microsoft says about it:

In this release, the C# Interactive Window is back in Visual Studio, as well as the command-line C# REPL window. (The Interactive Window is the REPL window inside Visual Studio.)

We've also released scripting APIs that enable you to build and run C# as a script. The scripting APIs are available on GitHub.

Additionally, we've released csi.exe, which is a tool that you can use to run a C# script file (.csx) from the Developer Command Prompt. For example, simply type csi myScript.csx to run your script file. Or, you can enter the command-line REPL mode to interactively evaluate snippets of C# code. To get to this mode, run the command csi without any arguments from the Developer Command Prompt.

Reference: https://www.visualstudio.com/news/vs2015-update1-vs#Csharp

like image 34
Iron Python Avatar answered Sep 30 '22 12:09

Iron Python