从C#调用IronRuby与委托人
问题描述:
是否可以使用C#调用IronRuby方法,并使用委托作为参数,以使yield
可以工作?从C#调用IronRuby与委托人
下面给我一个错误的参数个数(1代表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的块不是c#委托。
如果您将代理传递给Ruby,则应通过委托的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 = [email protected]();
t.test(new Action<string>(Console.WriteLine));
我们能否转换C#的委托到红宝石块......我不知道。
+1显示如何从IronRuby调用C#委托。仍然很好奇,如果有一种方法可以从IronRuby产生C#代码。 – 2010-11-28 20:22:41