c# - 使用委托(delegate)从 C# 调用 IronRuby

标签 c# delegates ironruby yield

是否可以通过 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/

相关文章:

c# - .NET 委托(delegate)是否用于事件?

c# - 以编程方式设置打印机功能

ios - 无法在swift中使用委托(delegate)调用另一个类方法

c# - 如何将 Iron ruby​​ 嵌入到 C# 程序中?

.net - Ruby 使用 native 工具包进行跨平台开发的可行性?

c# - 输入不是有效的 Base64 字符串,因为它包含非 Base 64 字符

c# - XDocument.Save() 没有标题

c# - 使用 delegate/BeginInvoke 在 C# 中将调用者与被调用者解耦

ios - 我的协议(protocol)和委托(delegate)不起作用?

c# - 使用 IronRuby 或 IronPython 修改 C# 对象列表