C# Console/CLI Interpreter?

≯℡__Kan透↙ 提交于 2019-12-18 10:40:24

问题


I wonder if there is something like a standalone Version of Visual Studios "Immediate Window"? Sometimes I just want to test some simple stuff, like "DateTime.Parse("blah")" to see if that works. But everytime i have to create a new console application, put in my code and test it.

The Immediate Window sadly only works when I am debugging something. Could PowerShell do that? Just open a CLI similar to what cmd.exe does, allowing me to execute some C# code?


回答1:


Linqpad - I use it like this all the time. http://www.linqpad.net/

Don't be misled by the name - that just describes the original motivation for it, not its functionality.

Just recently he released a version with proper statement completion - that's a chargeable add-on (the core tool is free), but a minute amount of money and well worth it, I think.




回答2:


The Mono project includes an interactive C# shell, this may be just what you're looking for.

http://www.mono-project.com/CsharpRepl




回答3:


C# Interactive window and csi.exe REPL were added to Visual Studio 2015 Update 1:

Introducing Interactive

The Interactive Window is back! The C# Interactive Window returns in Visual Studio 2015 Update 1 along with a couple other interactive treats:

  • C# Interactive. The C# Interactive window is essentially a read-eval-print-loop (REPL) that allows you to play and explore with .NET technologies while taking advantage of editor features like IntelliSense, syntax-coloring, etc. Learn more about how to use C# Interactive on Channel 9 or by reading our beginner’s walkthrough.

  • csi. If you don’t want to open Visual Studio to play around with C# or run a script file, you can access the interactive engine from the Developer Command Prompt. Type csi /path/myScript.csx to execute a script file or type simply csi to drop inside the command-line REPL.

  • Scripting APIs. The Scripting APIs give you the ability to execute snippets of C# code in a host-created execution environment. You can learn more about how to create your own C# script engine by checking out our code samples.

See What’s New in Visual Studio 2015 Update 1 for .NET Managed Languages.

Basically, now you have:

  • IDE REPL — C# Interactive window in VS
  • Script interpreter — csi foo.csx from Dev Cmd Prompt
  • Command line REPL — csi from Dev Cmd Prompt
  • Scripting API



回答4:


Try scriptcs, it's not integrated into the VS IDE but it does let you type and run C# in a script window without the need for a project compiler etc...




回答5:


Well, this isn't a direct answer to your question, but you could look at this tool:

  • Snippet Compiler

Also, if you want to see the IL produced, or similar, there is a tool that plugs into Reflector, called Snippy, based on the Snippy tool that Jon mentions in his own answer further down.

All of these are very nice to use.




回答6:


As you suggest, PowerShell can do what you want. For example, to test your DateTime.Parse, the following one liner will do the trick:

PS C:\Documents and Settings\Dan> [System.DateTime]::Parse("Blah")
Exception calling "Parse" with "1" argument(s): "The string was not recognized as a valid DateTime. There is a unknown word starting at index 0." At line:1 char:25 + [System.DateTime]::Parse( <<<< "Blah")

PS C:\Documents and Settings\Dan> [System.DateTime]::Parse("1/2/3")

01 February 2003 00:00:00

Note that the above uses the current release of PowerShell (v1.0). The next version of PowerShell will allows you to intermingle C# with PowerShell scripts more directly. To whet your appetite, watch this 7 minute screencast "C# to PowerShell" by Doug Finke. Very impressive!




回答7:


If you're using Mono, there's this:

CsharpRepl

Don Box hacked something very simple up a few years ago too.




回答8:


Along the lines of lassevk's answer, I've got "Snippy". This was developed for C# in Depth, and the UI is pretty rubbish, but it works - and lets you write extra members (methods, nested classes etc) as well, e.g.

public static void Foo()
{ 
    Console.WriteLine("Hello");
}
...
Foo();

(The ... is used to tell Snippy "everything under here belongs in Main".)




回答9:


We've just released CShell a full featured C# REPL IDE. It supports code completion, script files, adding references and is really extensible. Also we plan to add NuGet support soon, which will make it super quick to write some code and see how it works.

http://cshell.net/

We love LINQPad but it doesn't have a REPL, the code is executed once and you cannot do anything further with the results unless you modify the script and run the whole script again. This is okey, but sometimes if you want even more a scripty feeling then to evalute your code in a REPL is really nice and convenient.




回答10:


I also find that SharpDevelop is so quick and lightweight that it is the easiest way to whip off a quick test project.




回答11:


You may find the Object Test Bench useful. It's not very well known, but lets you create instances of classes, execute static methods and so on. It can be useful for discovering how to use unfamiliar APIs or for quick debugging of your own classes and methods, saving the creation of a test harness for simple checks.

You can find the MSDN documentation here:

http://msdn.microsoft.com/en-us/library/c3775d98%28VS.80%29.aspx




回答12:


If you could wait a while.. it looks like there could be a C# equivalent of Ruby's irb in time for C# 4.0 Anders H. demonstrated an interactive console session where you could type in arbitrary C# code and see results in his 'Future of C#' piece at PDC 2008. You could even pop a WPF Window from it and then play with it via the console interface. Pretty cool.




回答13:


Use LINQPad.

Name notwithstanding, it can execute any C# or VB code, from simple expressions to entire classes.

Plus, it can visualize entire object graphs in the results.

You can even add references to your own assemblies.




回答14:


Try the C# REPL Script Environment that is part of the O2 Platform. It is exactly what you are asking for.

It will give you a perfect environment to try out all C# features and APIs (conceptually the O2 REPL environment is similar to LinqPAD)

You should also take a look at Roslyn from Microsoft. On Multiple Roslyn based tools (all running Stand-Alone outside VisualStudio) , the first one is a simple Roslyn REPL




回答15:


If you happen to know and like Python, then IronPython may be a good alternative. It comes with a (Python) REPL console (ipy.exe) where you can import and use all of the .Net types.

I find it useful for testing out little things, exactly like DateTime.Parse("Blah").

Note that it can't actually execute C# code, but if all you want is access to .Net, then it's perfect. Also, if you install the IronPython Tools for VS, you can start a REPL session right in VS using a single keyboard shortcut (Alt+I) and leave it running in a docked window for when you need it.




回答16:


Update: http://avillenas.com/post/interactive-c

Well, you can try this.

  1. Download and install dotnet.github.io
  2. Open cmd
  3. Type "dotnet repl"
  4. Type whatever you need.

I did what you wanted to do. Click here to see the gif




回答17:


The Roslyn project sources contain a REPL called CSI (http://source.roslyn.io/#csi/Csi.cs). The Csi class is currently internal but with the the “csi” project (Roslyn.sln: Interactive/Hosts/csi) an executable console application is available that supports e. g. the command #r to load an assembly and #load to load and execute script files (start the csi and use #help).



来源:https://stackoverflow.com/questions/337254/c-sharp-console-cli-interpreter

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