C#的CSREPL-REPL
原文:CSREPL - REPL for C#
在ChrisAn忙於將Python寄宿於AvPad之時,我在思考為什麼他不把C#也同時考慮進去呢?
作為一項生存能力的測試,我在10分鐘內,編寫了一個簡單的REPL程式,它支援C#表示式和語句的命令列解析。
下面是它的基本使用方法:
>1+2+3
6
>DateTime.Now.ToString("T");
4:12:36 PM
為了支援交叉表示式變數,我定義了兩個內建的函式,Set 和 Get:
>Set("X",32)
32
>Get("X")
32
為了支援呼叫任意的程式碼塊,我定義了一個能夠解析void(void) 代理的 Invoke方法:
> Invoke(delegate { for (int i = 0; i < 6; i++) Console.WriteLine(i); })
0
1
2
3
4
5
下面是原始碼,在Whidbey Beta2下,將其編譯為Console程式。
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;
namespace csrepl {
class Program {
static string funcprefix = "using System; "
+ "public delegate void Proc(); "
+ "public class Wrapper { "
+ " public static object Set(string name, object value) { "
+ " AppDomain.CurrentDomain.SetData(name, value); "
+ " return value; "
+ " } "
+ " public static object Get(string name) { "
+ " return AppDomain.CurrentDomain.GetData(name); "
+ " } "
+ " public static object Invoke(Proc proc) { "
+ " proc(); "
+ " return null; "
+ " } "
+ " public static object Eval() { return ";
static string funcsuffix = "; } }";
static string StringEval(string expr) {
string program = funcprefix + expr + funcsuffix;
ICodeCompiler compiler = CodeDomProvider.CreateProvider("C#").CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
CompilerResults results = compiler.CompileAssemblyFromSource(cp, program);
if (results.Errors.HasErrors) {
if (results.Errors[0].ErrorNumber == "CS0029")
return StringEval("Invoke(delegate { " + expr + "; })");
return results.Errors[0].ErrorText;
}
else {
Assembly assm = results.CompiledAssembly;
Type target = assm.GetType("Wrapper");
MethodInfo method = target.GetMethod("Eval");
object result = method.Invoke(null, null);
return result == null ? null : result.ToString();
}
}
static void Main(string[] args) {
while (true ) {
Console.Write("> ");
Console.Out.Flush();
string expr = Console.ReadLine();
if (expr == null)
break;
try {
string result = StringEval(expr);
Console.WriteLine(result);
}
catch (TargetInvocationException ex) {
Console.WriteLine(ex.InnerException.GetType().Name + ": " + ex.InnerException.Message);
}
catch (Exception ex) {
Console.WriteLine(ex.GetType().Name + ": " + ex.Message);
}
}
}
}
}
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.CodeDom;
using System.CodeDom.Compiler;
namespace csrepl {
class Program {
static string funcprefix = "using System; "
+ "public delegate void Proc(); "
+ "public class Wrapper { "
+ " public static object Set(string name, object value) { "
+ " AppDomain.CurrentDomain.SetData(name, value); "
+ " return value; "
+ " } "
+ " public static object Get(string name) { "
+ " return AppDomain.CurrentDomain.GetData(name); "
+ " } "
+ " public static object Invoke(Proc proc) { "
+ " proc(); "
+ " return null; "
+ " } "
+ " public static object Eval() { return ";
static string funcsuffix = "; } }";
static string StringEval(string expr) {
string program = funcprefix + expr + funcsuffix;
ICodeCompiler compiler = CodeDomProvider.CreateProvider("C#").CreateCompiler();
CompilerParameters cp = new CompilerParameters();
cp.GenerateExecutable = false;
cp.GenerateInMemory = true;
CompilerResults results = compiler.CompileAssemblyFromSource(cp, program);
if (results.Errors.HasErrors) {
if (results.Errors[0].ErrorNumber == "CS0029")
return StringEval("Invoke(delegate { " + expr + "; })");
return results.Errors[0].ErrorText;
}
else {
Assembly assm = results.CompiledAssembly;
Type target = assm.GetType("Wrapper");
MethodInfo method = target.GetMethod("Eval");
object result = method.Invoke(null, null);
return result == null ? null : result.ToString();
}
}
static void Main(string[] args) {
while (true ) {
Console.Write("> ");
Console.Out.Flush();
string expr = Console.ReadLine();
if (expr == null)
break;
try {
string result = StringEval(expr);
Console.WriteLine(result);
}
catch (TargetInvocationException ex) {
Console.WriteLine(ex.InnerException.GetType().Name + ": " + ex.InnerException.Message);
}
catch (Exception ex) {
Console.WriteLine(ex.GetType().Name + ": " + ex.Message);
}
}
}
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1726369
相關文章
- [C#]C#中字串的操作C#字串
- C# RabbitMQ的使用C#MQ
- C# ExpandoObject的使用C#Object
- C#中的集合C#
- C#中的delegateC#
- 可怕的 C# (轉)C#
- C# ArrayList的用法C#
- C# peek()的用法C#
- C#中的MVCC#MVC
- C#中的MVVMC#MVVM
- 【C#開發】C#的協變和逆變C#
- 理解C#中的ValueTaskC#
- C#的委託案例C#
- 瞭解C#的ExpressionC#Express
- C# 10的新特性C#
- 冪的計算(C#)C#
- C#樹的實現C#
- C#中的多型C#多型
- C# 6.0 的新特性C#
- C#中委託的呼叫C#
- C#中#region,#if的作用C#
- C#中WebBrowser的使用C#Web
- c#泛型的使用C#泛型
- C#裡List的用法C#
- C# 壓縮的缺陷C#
- C#的筆記~TWOC#筆記
- C#中Enum的用法C#
- C#中的委託C#
- 重學c#系列——對c#粗淺的認識(一)C#
- 【C#】Learn C# in X minutesC#
- 用c#建立與資料庫的連線 c#連sqlserverC#資料庫SQLServer
- 適合C# Actor的訊息執行方式(2):C# Actor的尷尬C#
- [C#]C#時間日期操作C#
- C#呼叫 C++的DLLC#C++
- C# 中的特性 AttributeC#
- C#中屬性的解析C#
- 如何使用 C# 中的 LazyC#
- C# 6.0的Dictionary語法C#