是否可以通过 yield
起作用的方式从 C# 调用 IronRuby 方法,将委托(delegate)作为参数?
下面给出了一个错误数量的参数(1 委托(delegate) 0) 异常。
Action<string> action = Console.WriteLine;
var runtime = Ruby.CreateRuntime();
var engine = runtime.GetEngine("rb");
engine.Execute(@"
class YieldTest
def test
yield 'From IronRuby'
end
end
");
object test = engine.Runtime.Globals.GetVariable("YieldTest");
dynamic t = engine.Operations.CreateInstance(test);
t.test(action);
最佳答案
我确定 Ruby 的 block 不是 c# 委托(delegate)。
如果您将委托(delegate)传递给 Ruby,您应该通过委托(delegate)的 Invoke 方法调用它。
这是示例代码:
var rt = Ruby.CreateRuntime();
var eng = rt.GetEngine("rb");
eng.Execute(@"
class Blocktest
def test(block)
block.Invoke('HELLO From IronRuby')
end
end
");
dynamic ruby = eng.Runtime.Globals;
dynamic t = ruby.Blocktest.@new();
t.test(new Action<string>(Console.WriteLine));
我们能否将 c# 委托(delegate)转换为 ruby block ...我不知道。
关于c# - 使用委托(delegate)从 C# 调用 IronRuby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4189955/