1、简介
能够动态执行 C# 代码是一件很酷的功能,比如,我们可以在控制台中输入一行 C# 代码,然后程序自动编译并执行这一行代码,将结果显示给我们。这差不多就是一个最简单的 C# 代码解释器了。
动态执行 C# 代码又是一件很有用的功能,比如,我们可以将某些代码写在某个文件之中,由程序集在执行时进行加载,改变这些代码不用中止程序,当程序再次加载这些代码时,就自动执行的是新代码了。
下面,我将在写一个简单C# 代码解释器,然后将在 C# 代码解释器之中加入动态代码与解释器环境间的动态交互机制,来演示一个很好很强大的应用。
2、简单的 C# 代码解释器
关于如何动态执行 C# 代码在 Jailu.Net 的《如何用C#动态编译、执行代码》一文中讲述的很清晰。采用该文所述方式写一个 C# 代码解释器:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Collections.Generic;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Reflection;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Globalization;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using Microsoft.CSharp;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.CodeDom;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.CodeDom.Compiler;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.IO;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Xml;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace Test
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
class Program
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
static void Main(string[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.Write(">> ");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String cmd;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Context cxt = new Context();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
while ((cmd = Console.ReadLine().Trim()) != "exit")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!String.IsNullOrEmpty(cmd))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cxt.Invoke(cmd);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.Write("\n>> ");
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public class Context
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
public CSharpCodeProvider CodeProvider
![](https://www.cnblogs.com/Images/dot.gif)
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
public IDictionary<String, Assembly> Assemblys
![](https://www.cnblogs.com/Images/dot.gif)
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Context()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
CodeProvider = new CSharpCodeProvider(new Dictionary<string, string>()
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/dot.gif)
{ "CompilerVersion", "v3.5" } });
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Assemblys = new Dictionary<String, Assembly>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Assembly[] al = AppDomain.CurrentDomain.GetAssemblies();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (Assembly a in al)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
AddAssembly(a);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(CurrentDomain_AssemblyLoad);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private void AddAssembly(Assembly a)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (a != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Assemblys.Add(a.FullName, a);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void CurrentDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Assembly a = args.LoadedAssembly;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!Assemblys.ContainsKey(a.FullName))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
AddAssembly(a);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public CompilerParameters CreateCompilerParameters()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
CompilerParameters cp = new CompilerParameters();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cp.GenerateExecutable = false;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cp.GenerateInMemory = true;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (Assemblys != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (Assembly a in Assemblys.Values)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cp.ReferencedAssemblies.Add(a.Location);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return cp;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Invoke(String cmd)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String inputCmdString = cmd.Trim();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (String.IsNullOrEmpty(inputCmdString)) return;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String fullCmd = BuildFullCmd(inputCmdString);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
CompilerResults cr = CodeProvider.CompileAssemblyFromSource(CreateCompilerParameters(), fullCmd);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (cr.Errors.HasErrors)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Boolean recompileSwitch = true;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (CompilerError err in cr.Errors)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//CS0201 : Only assignment, call, increment, decrement, and new object expressions can be
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//used as a statement
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!err.ErrorNumber.Equals("CS0201"))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
recompileSwitch = false;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
break;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 重新编译
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (recompileSwitch)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String dynaName = "TempArg_Dynamic_" + DateTime.Now.Ticks.ToString();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
inputCmdString = String.Format(" var {0} = ", dynaName) + inputCmdString;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
inputCmdString += ";\n System.Console.WriteLine(" + dynaName + ");";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
fullCmd = BuildFullCmd(inputCmdString);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cr = CodeProvider.CompileAssemblyFromSource(CreateCompilerParameters(), fullCmd);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (cr.Errors.HasErrors)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("编译错误:");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (CompilerError err in cr.Errors)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine(err.ErrorNumber);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine(err.ErrorText);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Assembly assem = cr.CompiledAssembly;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Object dynamicObject = assem.CreateInstance("Test.DynamicClass");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Type t = assem.GetType("Test.DynamicClass");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
MethodInfo minfo = t.GetMethod("MethodInstance");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
minfo.Invoke(dynamicObject, null);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String BuildFullCmd(String inputCmdString)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String fullCmd = String.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
fullCmd += @"
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
namespace Test
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public class DynamicClass
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void MethodInstance()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
" + inputCmdString + @";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return fullCmd;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
编译执行后就得到一个傻傻的 C# 代码解析器,也可以当一个简单的计算器用:
3、解释器与所解释的代码之间进行变量交互
如果将所解释的代码中的某些变量储存下来,供给以后的代码用,这一解释器的功能又会强大很多。假设这类变量名称以$打头,如:
$myblogname = “http://xiaotie.cnblogs.com”
将在解释器环境中定义(如果该变量未存在)或赋值于(如果该变量已存在)一个名为 myblogname 的字符串变量,指向字符串“http://xiaotie.cnblogs.com”。而,System.Console.WriteLine($myblogname)则取出并打印出字符串该变量所引用的。
简单说来,也就是让所解释的代码中能够初始化并引用解释器中的变量。
如何实现呢?这是本文的重点。
首先,在 Context 类中定义一个SortedDictionary储存变量,并提供索引访问:
BuildFullCmd方法改变为:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
private String BuildFullCmd(String inputCmdString)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String fullCmd = String.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
fullCmd += @"
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
using Test;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public class DynamicClass
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Context m_context;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void MethodInstance(Context context)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
m_context = context;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
" + inputCmdString + @";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return fullCmd;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
这样,在动态生成的对象中,便可以引用Context对象。
对于inputCmdString 中未定义的外部变量,在第一次遇见时将$argname替换为一个随机生成的内部变量,在代码的最后,将这个内部变量储存在 Context 中。
虽然通过 (Context[argname].GetType())(Context[argname]) 便可引用外部变量 $argname,但是这样引用赋值时,编译器会报错。解决这个问题需要一个新的类:
将inputCmdString中的外部变量$argname统一替换为(new ObjectHelper <m_context[“argname”].GetType()> (m_context, “argname”)).Obj" 即可实现在动态代码中对已定义外部变量的引用。
上述对inputCmdString的预处理代码为:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Regex re;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 处理未初始化的环境变量
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
re = new Regex(@"^(\$)(\w)+");
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
if (inputCmdString != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Match m = re.Match(inputCmdString);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (m != null && m.Length > 1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String outArgName = inputCmdString.Substring(m.Index, m.Length).Substring(1);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (this[outArgName] == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String innerArgName = "TempArg_" + outArgName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
inputCmdString = "var " + inputCmdString.Replace("$" + outArgName, innerArgName);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
inputCmdString += ";m_context[\"" + outArgName + "\"]=" + innerArgName + ";";
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
// 处理其它环境变量
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
re = new Regex(@"(\$)(\w)+");
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IDictionary<String, String> ArgsList = new Dictionary<String, String>();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
if (inputCmdString != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
MatchCollection mc = re.Matches(inputCmdString);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (mc != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (Match m in mc)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (m.Length > 1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String outArgName = inputCmdString.Substring(m.Index, m.Length).Substring(1);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!ArgsList.ContainsKey(outArgName))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Object obj = this[outArgName];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (obj == null) throw new Exception("不存在环境变量" + outArgName);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String innerArgName = String.Format(@"(new ObjectHelper<{0}>(m_context,""{1}"")).Obj", obj.GetType(), outArgName);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ArgsList.Add(outArgName, innerArgName);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (String outArg in ArgsList.Keys)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/dot.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
inputCmdString = inputCmdString.Replace("$" + outArg, ArgsList[outArg]);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
这里做了个简化,即定义外部变量的格式必须为 $argname = value,其中 $argname 必须在行首。
这样,对于:$myblogname = "http://xiaotie.cnblogs.com". 因为 myblogname 变量不存在,被解析为:
var TempArg_myblogname = "http://xiaotie.cnblogs.com";
m_context["myblogname"]=TempArg_myblogname;;
定义后,当再出现 $myblogname,则被解析为 (new ObjectHelper<System.String>(m_context,"myblogname")).Obj;
看看实际执行情况:
完整代码于此下载。
4、一个很好很强大的应用—---打入.Net 程序内部,看看其执行情况。
采用上面的方法改进了 OrcShell(OrcShell详情见我前面的随笔: 实现简单的CSharpShell -- OrcShell )。新版 OrcShell 程序于此下载(需要.Net 3.5)。基本上是一个可用的 小型 .Net Framework Shell 了,可以动态的查看、创建、执行 .Net 的类型了。不过,自动提示与完成功能还没有做,使用起来还是较不方便的。
help 指令可以查看常用指令列表:
lsc 列出当前命名空间中的类型和下属命名空间。格式: lsc [name]
dirc 同 lsc
cdc 改变当前的命名空间,格式: cdc [.|..|name]
my 查看全部变量。格式:my。可通过$ArgName来引用变量。
alias 查看全部别名。格式:alias
use 添加命名空间。格式: use [namespace]
unuse 移除命名空间。格式:unuse [namespace]
import 导入程序集,有两种导入方式: "import -f [fullpath]","import [partname]"