代码之家  ›  专栏  ›  技术社区  ›  Jonas Elfström

用委托从C#调用IronRuby

  •  5
  • Jonas Elfström  · 技术社区  · 14 年前

    是否可以从C调用IronRuby方法,并将委托作为参数 yield 会有用吗?

    下面给我一个 参数数目错误(0为1) 例外。

    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);
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   Sergey Mirvoda    14 年前

    我确信Ruby的block不是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 = ruby.Blocktest.@new();
    t.test(new Action<string>(Console.WriteLine));
    

    我们能把c#委托转换成ruby块吗。。。我不知道。

        2
  •  1
  •   Jonas Elfström    14 年前

    得到了答案 IronRuby core team 成员 Tomáš MatouÅ¡ek 在IronRuby核心列表中,这是不可能的。但是。