2010-11-15 18 views
5

È possibile chiamare un metodo IronRuby da C# con un delegato come parametro in modo che lo yield funzioni?Chiamare IronRuby da C# con un delegato

Quanto segue mi dà un numero errato di argomenti argomenti (1 per 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); 

risposta

1

Sono sicuro che il blocco di Ruby non è un delegato C#.
Se si passa delegato a Ruby, è necessario richiamarlo tramite il metodo Invoke del delegato.
Ecco il codice di esempio:

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 = [email protected](); 
t.test(new Action<string>(Console.WriteLine)); 

Can convertiamo C# delegato nel blocco rubino ... non lo so.

+0

+1 per mostrare come richiamare i delegati C# da IronRuby. Ancora curioso di sapere se esiste un modo per ottenere un codice C# da IronRuby. –