2013-02-08 61 views
0

如何在IronRuby下的ScriptScope上设置委托? 我试过上面的代码,但在调用函数时出现了一个ArgumentException。如何在ScriptScope上设置功能

scope.SetVariable("import", new Action<string>(DSLImport)); 

import "Data" 

另外,我怎样才能使用上面的代码发送块作为回调到C#代码?

import "Data" do |f| 
    f.foo = false 
end 

回答

0

我发现了一种可能不是最好的方式,但工作。 这是ScriptScope的扩展方法:

public static void SetMethod(this ScriptScope scope, string name, Delegate method) 
{ 
    scope.SetVariable(name + "__delegate", method); 
    scope.Engine.Execute("def " + name + "(*args, &block)\nargs.push block if block != nil\n" + name + "__delegate.invoke(*args)\nend", scope); 
}