【问题标题】:How to set function on ScriptScope如何在 ScriptScope 上设置功能
【发布时间】:2013-02-08 21:16:48
【问题描述】:

如何在 IronRuby 下的 ScriptScope 上设置委托? 我尝试了上面的代码,但在调用函数时出现了 ArgumentException。

scope.SetVariable("import", new Action<string>(DSLImport));

import "Data"

另外,如何使用上述代码将块作为回调发送到 C# 代码?

import "Data" do |f|
    f.foo = false
end

【问题讨论】:

    标签: c# .net clr ironruby


    【解决方案1】:

    我找到了一种可能不是最好但有效的方法。 它是 ScriptScope 的扩展方法:

    public static void SetMethod(this ScriptScope scope, string name, Delegate method)
    {
        scope.SetVariable(name + "__delegate", method);
        scope.Engine.Execute("def " + name + "(*args, &block)\nargs.push block if block != nil\n" + name + "__delegate.invoke(*args)\nend", scope);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 2015-02-13
      • 2013-05-15
      • 2019-09-10
      相关资源
      最近更新 更多