我使用IronRuby和試圖想出一個無縫傳遞給C#塊。這個問題是從How to use an IronRuby block in C#開始的,它指示塊不能在IronRuby和C#之間傳遞。使用的IronRuby的包裝方法得到一個塊到C#
我的後續問題是是否有到acheive使用Ruby的包裝方法,使塊爲lambda或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 "));
}
}
很棒,thx! – sipwiz 2010-02-28 23:21:37