不完全是 Ruby,下面是我对 IronPython 所做的(确保首先引用程序集):
runtime = IronPython.Hosting.Python.CreateRuntime();
}
public void Run(string script, params object[] variables)
{
var scriptSource = runtime.GetEngineByFileExtension("py").CreateScriptSourceFromString(script, SourceCodeKind.Statements);
var scope = runtime.GetEngineByFileExtension("py").CreateScope();
foreach (var variable in variables)
foreach (var property in variable.GetType().GetProperties())
if (property.CanRead)
scope.SetVariable(property.Name, property.GetValue(variable, null));
scriptSource.Execute(scope);
这里最重要的元素是脚本引擎。您可以通过不同的选项(在我的情况下为扩展)请求它,并从内存或磁盘源中创建脚本。
也可以共享变量,这也显示了。
我认为现在在 C# 4.0 中有更好的方法,因为 C# 本身已成为一种动态编程语言。