[英]Calling IronRuby from C# with a delegate
是否可以使用委托作為參數從C#調用IronRuby方法,以便yield
可以工作?
以下給出了錯誤的參數數量(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的塊不是ac#delegate。
如果將委托傳遞給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 = ruby.Blocktest.@new();
t.test(new Action<string>(Console.WriteLine));
我們可以將c#delegate轉換為ruby塊嗎...我不知道。
IronRuby核心團隊成員TomášMatoušek在IronRuby核心列表上得到了答案,這是不可能的。 然而。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.