// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using Microsoft.Scripting.Hosting; using IronPython.Hosting; namespace M2M { public class Program { public class Data { public int Iv; public string Sv; public Data(int iv, string sv) { this.Iv = iv; this.Sv = sv; } public void M() { Iv++; Sv += "X"; } public override string ToString() { return string.Format("({0},{1})", Iv, Sv); } } public static void Test(String language, String source) { Console.WriteLine(language); Console.WriteLine(source); ScriptRuntime srt = ScriptRuntime.CreateFromConfiguration(); ScriptEngine se = srt.GetEngine(language); ScriptScope ss = se.CreateScope(); ss.SetVariable("n", 3); ss.SetVariable("s", "Hi"); ss.SetVariable("o", new Data(123, "ABC")); se.Execute(source, ss); Data o = (Data)ss.GetVariable("o"); Console.WriteLine(o); } public static void Main(String[] args) { Test("Python", "for i in range(n):\r\n" + " print('Python says: ' + s)\r\n" + "o.Iv = o.Iv + 1\r\n" + "o.Sv = o.Sv + 'X'\r\n" + "o.M()"); Test("Ruby", "for i in 1..n do\r\n" + " puts 'Ruby says: ' + s\r\n" + "end\r\n" + "o.Iv = o.Iv + 1\r\n" + "o.Sv = o.Sv + 'X'\r\n" + "o.M()"); } } }