CShell (REPL) :轻量级、可扩展(dll)、 支持C#语法 的接口算法验证工具

发现一个开源小工具——CShell

 

CShell方便进行各种简单的C#代码测试,比如接口验证,算法验证等等。完成相应工作 Visual Studio往往太重。该工具基于NRefactory、Mono.CSharp和Roslyn等项目开发完成。

  1. CShell + Masuit.Tools =》多功能工具箱
  2. CShell + HslCommunication =》 Modbus、Serial、Profinet 等 工控行业通讯协议调试工具箱
  3. CShell + RestSharp =》 WebApi调试工具箱
  4. CShell + DynamicExpresso =》 灵活又强大的表达式计算器
  5. CShell + Rivers =》图论概念学习与验证
  6. CShell + FileDB =》文件容器测试

 

什么是CShell?

CShell是一个交互式的C#脚本环境。 它允许你在一个类似于控制台的环境中使用C#,而不需要任何繁琐的操作,这个环境被称为read-eval-print-loop(REPL)。 代码直接在shell窗口中编译和执行,不需要单独的可执行文件被编译,然后在不同的进程中运行。 更为复杂的代码可以用C#脚本编写,然后对一个文件、只有一行或部分选择的代码进行编译。 然后,您的结果可以输出到HTML(如LINQPad)或可以数据表或绘图形式查看。

CShell是探索数据和快速深入了解信息的完美工具,因为你可以写一行代码,执行它,检查结果,然后再写一些代码,再次执行。

如果你只是想测试一两行C#代码,而不需要在Visual Studio中创建一个新的控制台项目,只需启动CShell,键入你想测试的代码,你就可以立即看到它是否以你想要的方式工作。

如下图所示,界面可分为菜单栏、工具栏、脚本编辑区域、交互窗口和工作空间浏览器。

 

功能

Read Eval Print Loop (REPL)。 可便捷地评估C#代码(使用Mono.CSharp构建)。 包括直接在命令行中完成代码。

C#代码编辑器。 一个强大的编辑器,具有代码完成功能,支持C#脚本(.csx文件)和普通C#(.cs)文件以及更多的文件类型。 脚本中的代码可以直接发送到REPL。

工作空间浏览器(WorkSpace Explorer)。 在这里,你可以组织所有的脚本和其他文件,管理引用,并编辑".cshell"。 文件,在该文件中,整个CShell IDE可以使用C#代码(而不是XML)进行配置。

扩展CShell。 整个项目在构建时就考虑到了可扩展性,添加新的编辑器或汇等模块可以在CShell中快速开发和加载。

 

入门

要使用CShell工作,你需要创建一个工作空间,这个工作空间是相对永久性的,即你不必为你所做的每件事都创建一个新的工作空间。 把它看作是你的脚本所在的一般环境。 要创建工作区。 文件 > 新工作区...

现在你已经准备好开始使用REPL了,只需开始输入C#代码并按下回车键。可以在CShell目录下找到脚本教程: CShell/Templates/Tutorial.csx。

 

功能演示

基本的数学和逻辑运算

 

脚本示例  

// CShell Tutorial
// ===================
//
// CShell is an interactive C# environment, unlike most IDEs or other development setups which compile their 
// code into an executable and then run it, CShell uses a read-evaluate-print-loop (REPL) to execute all code which 
// perserves the state from one execution to the next.
// 
// This tutorial will guide you through the basic possibilities that this application offers. If you know what a REPL 
// loop is already and just want to go to the nitty gritty stuff scroll down to the "CShell Specific" section.

// ===================
// Getting Started
// ===================

// To evaluate an expression in the REPL shell enter following code in the window bellow an press enter: "7*7"
// As you can see the expression is evaluated and the result is printed.

// The console output is forwarded to the REPL as well. Try entering: "Console.WriteLine("Hello CShell");"

// When you press the up arrow you can iterate through the command history.

// Not all code has to be typed in the REPL window. Code in any file can be sent to the REPL and be evaluated.
// To do this just put the curor to the line you want to evaluate and press Alt+Enter. Try it here:
Math.Pow(2, 4);

// Also, more than one line can be executed at once. Select the the two line below and hit Alt+Enter.
var x = 7;
x + x;

// The state is preserved now just enter "x" in the REPL and hit enter: The variable "x" containing the value 7 is still there.

// Methods can be declared too, but only in classes. Select the class and send it to the REPL.
// Select lines 35-45 and press Alt+Enter, to evaluate the class an make it available for later.
/*
static class MyMath
{
	public static long Fibonacci(long n)
    {
    	//anything more than 48 will result in a stack overflow.
    	if (n >= 48) throw new ArgumentException("Enter a number less than 48");
        if (n == 0) return 0;
        if (n == 1) return 1;
        return Fibonacci(n - 1) + Fibonacci(n - 2);
    }
}
*/
// Now the method can be called like so:
MyMath.Fibonacci(12);

// If you want only the method it can also be declared with a lamda:
Action<int> multiplyAndPrint = n => Console.WriteLine(n * 2);
//and then called like this:
multiplyAndPrint(7);

// The above fibonacci method would go something like this:
Func<int, int> fib = null;
fib = n => n > 1 ? fib(n - 1) + fib(n - 2) : n;
// Then call it
fib(12);

// By now you might already have seen it, but if an error occurs in the execution the result is red and compiler warnings are yellow.
throw new Exception("hahaha");


// ===================
// CShell Specific
// ===================
// At the root of the CShell environment is the .cshell file. If you double click it, you'll see that it contains C# code and not XML or 
// some other data definitions. It's structured according to a simple convention, it needs to implement a class called "CShellFile" and 
// the interface ICShellFile which containt two methods: "OnOpened" and "OnClosing". The whole workspace environment can be configured
// and saved in those two methods. That the file itself contains normal C# code is an important point to understand about CShell, all
// APIs like saving things, restoring the layouts and workspace files, modifying the UI are available to you at anytime, when opening a
// workspace or later via the REPL.

// Your workspace will probably not change very much as compared to, for example, VisualStudio solutions. It's recommened to configure your
// environment once via the .cshell file and use it for whatever work you wanna do. Of course if a different project requires you to load
// many different assemblies you might want to create a different workspace for that.
// When CShell is opened the last .cshell file will be loaded automatically unless you provide a different .cshell file in the arguments.

// All the APIs live in the "CShell" namespace. To try this just type "CShell" in the REPL and see what's available. One of the most 
// important static classes is called "Shell", through it many UI APIs can be accessed.
// for example to update the status when a long running operation is executed we can do the following:
for(int i=0;i<100;i++)
{
	System.Threading.Thread.Sleep(20);
	//this will update the status bar
	CShell.Shell.UpdateProgress(i);
}

// Note: The code is NOT executed in a seperate AppDomain that cannot contaminate the UI shell. The CShell UI, the loaded assemblies and all 
// evaluated code live in the same AppDomain and can call each other. Of course this could lead to quite some trouble but it creates tremendous
// flexibility, because remember, this is a scripting envrionment.


// Dumping to Sinks
// ------------------------------
// To explore the data and outputs generated in CShell it's helpful to dump the data to a sink.
// The simplest and default sink is the Xhtml sink. To dump to any sink use the "Shell.Dump" overloads.
Shell.Dump(new []{1,2,3,4});

// When dumping data you can select a sink by specifying a sink URI which are always in following format:
//   "sink://<Provider>/<SinkName>/<SinkTitle>"
// All CShell sinks are using the provider name "cshell" so they have follwing format:
//   "sink://cshell/<SinkName>/<SinkTitle>"

// There are a few sinks suppored out of the box:
//  * xhtml: Uses a LinqPad like XHtml dumper.
    new []{1,2,3,4}.Dump(new Uri("sink://cshell/xhtml/MyXhtmlSink"));
//  * grid: Displays the data in a data grid.
    new []{1,2,3,4}.Dump(new Uri("sink://cshell/grid/MyGridSink"));
//  * plot: Plots the data points of an IEnumerable.
	new []{1,2,3,4}.Dump(new Uri("sink://cshell/plot/MyPlotSink"));

// Since it's quite annoying having to type the whole URI everytime you wanna dump some data, there are usually shortcut methods for a sink.
// For example to plot there are many helper methods:
   new []{5,4,3,2,4,5}.Plot(); // -> same as .Dump(new Uri("sink://cshell/grid"))
   Plotting.PlotFunction(Math.Sin, "SinFunction"); // -> not possible by just dumping
   Plotting.GetPlotSink(null).Clear(); // gets the default sink, called "Plot" and clears it

// Some other shortcuts are:
   new []{1,2,3,4}.DumpXhtml(); // -> same as .Dump(new Uri("sink://cshell/xhtml"))
   new []{1,2,3,4}.DumpGrid(); // -> same as .Dump(new Uri("sink://cshell/grid"))


// Scripting the UI and Workspace
// ------------------------------
// There are many built in helper methods in CShell, here we'll explore some of them. Call them in a script, class or directly in the .cshell file
// to configure the UI and workspace to your liking.

// Evaluate a string as code
Shell.Evaluate("1+1");
// Evaluate the contents of a file. It finds the first occurence of the file in the whole workspace and executes it.
Shell.TryEvaluateFile("SomeFileInYourWorkspace.cs");
// Note: I like to create a file called "Usings.cs" where I add all my "using" declarations used in the .csx scripts. Then I evaluate it the .chell 
// file's "OnOpened" event after loading the workspace like so: Shell.TryEvaluateFile("Usings.cs");

// Modify the REPL, you can access the IRepl interface via "Shell.Repl"
Shell.Repl.Clear();


// ===================
// Under the Hood
// ===================
// To evaluate the code we use the Mono.CSharp compiler as a service called "Evaluator". It basically compiles your code in memory and executes it 
// right away.

 

以上功能就可以进行算法验证了。当然CShell功能不限于此,还可以根据需要扩展。

添加第三方库,如Modbus库 :HslCommunication

点击工作空间浏览器的References

 

选择链接库

 

点击打开

 

编写脚本访问Modbus 服务

 

更多尝试,如果感兴趣欢迎一起探讨~~

附上下载地址:http://cshell.net  或者 https://github.com/lukebuehler/CShell

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值