我想在雇员再培训局系统中使用IronRuby来解析.erb格式的文件并获得输出。
在ruby中,这将类似于:
require "erb"
erbContent = "..."
return ERB.new(erbContent,0,"%<>").result但这在我的IronRuby项目中是行不通的。我得到了一个关于erb文件丢失的异常...所以我猜这是一个库问题。然后我用IronRuby目录的路径启动了我的Ruby引擎,然后抛出了一个不同的异常:
allocator undefined for System::String发布于 2012-01-11 06:48:26
我也遇到过类似的问题,但我通过作用域将字符串作为局部变量提供给脚本。本地变量是一个.NET CLR字符串,这是导致问题(please see here)的原因。
我的解决方案是使用to_s将传递给ERB.new的字符串转换为Ruby字符串。
下面是一个示例(Ruby代码片段):
require 'erb'
template = ERB.new(template_code.to_s)
template.result(binding)调用上述脚本的C#部件:
var scriptEngine = Ruby.CreateEngine();
var templateCode = "my ERB template code goes here";
// Pass the template code to the Ruby script through a scope
var scope = _scriptEngine.CreateScope(new Dictionary<string, object>()
{
{"template_code", templateCode}
});
var result scriptEngine.Execute(_boostrapScript, scope).ToString();在上面的C#代码片段中,_bootstrapScript是一个包含上面的Ruby代码片段的字符串。
https://stackoverflow.com/questions/8364490
复制相似问题