How do I enable C# scripting in VS 2015?

做~自己de王妃 提交于 2019-12-23 08:50:03

问题


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.


回答1:


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

Try the nightlies instead.




回答2:


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



来源:https://stackoverflow.com/questions/31632984/how-do-i-enable-c-sharp-scripting-in-vs-2015

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!