私はIronRubyを使用していて、ブロックをC#に渡すためのシームレスな方法を考え出そうとしています。この質問は、 C#でIronRubyブロックを使用する方法の続きであり、IronRubyとC#の間でブロックを渡すことができないことを示しています。
私の次の質問は、Rubyラッパーメソッドを使用してブロックをラムダまたはProcオブジェクトにし、それをC#に渡す方法で同じ目標を達成する方法があるかどうかについてです。
私がやりたいことの線に沿ったいくつかのコードを以下に示します。コードは機能しませんが、意図が十分に明確であることを願っています。
string scriptText =
@"def BlockTest
result = csharp.BlockTest somehow-pass-the-block ("hello")
puts result
end
BlockTest { |arg| arg + 'world'}";
Console.WriteLine("Compiling IronRuby script.");
ScriptEngine scriptEngine = Ruby.CreateEngine();
ScriptScope scriptScope = scriptEngine.CreateScope();
scriptScope.SetVariable("csharp", new BlockTestClass());
scriptEngine.Execute(scriptText, scriptScope);
C#BlockTestクラスは次のとおりです。
public class BlockTestClass
{
public void BlockTest(Func<string, string> block)
{
Console.WriteLine(block("hello "));
}
}