Advertisement
ForeverZer0

Embed IronRuby in C# App

Aug 20th, 2011
870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3. using Microsoft.Scripting;
  4. using Microsoft.Scripting.Hosting;
  5. using IronRuby;
  6.  
  7. // Add references to: IronRuby.dll, IronRuby.Libraries.dll,
  8. // Microsoft.Scripting.dll, and Microsoft.Scripting.Core.dll
  9.  
  10. namespace IronRubyEmbedDemo
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.             //Set the title of the form
  18.             this.Text = "Hello World!";
  19.             //Create the iron ruby engine
  20.             ScriptEngine engine = IronRuby.Ruby.CreateEngine();
  21.             ScriptScope scope = engine.CreateScope();
  22.             //Set this form as the variable "form" in the engine
  23.             scope.SetVariable("form", this);
  24.             //Create a line of code to execute in the engine
  25.             String code = "form.Text";
  26.             //Execute the code
  27.             ScriptSource script = engine.CreateScriptSourceFromString(code, SourceCodeKind.SingleStatement);
  28.             Object result = script.Execute(scope);
  29.             //Display the result
  30.             MessageBox.Show(result.ToString());
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement