private void ScriptInString() { Console.WriteLine("Running script from string..."); var code = @" class script2class: def Goodbye(self): message = 'Goodbye via the script!' return message def GoodbyeWithName(self, name): message = 'Goodbye ' + name return message def GoodbyeWithPerson(self, person): message = 'Goodbye ' + person.Name return message"; var compiled = new Script(); compiled.InitializeFromString(code); Console.WriteLine(compiled.Execute<string>("script2class", "Goodbye")); // single param call Console.WriteLine(compiled.Execute<string>("script2class", "GoodbyeWithName", "Paulo")); // pass instance of complex type var person = new Person { Name = "Paulo Mouat", Address = "Boston" }; Console.WriteLine(compiled.Execute<string>("script2class", "GoodbyeWithPerson", person)); // duck typing var hasNameLikePerson = new HasNameLikePerson { Name = "HAL", Model = 9000 }; Console.WriteLine(compiled.Execute<string>("script2class", "GoodbyeWithPerson", hasNameLikePerson)); Console.WriteLine(); }
private void ScriptInFile() { Console.WriteLine("Running script from file..."); var script = new Script(); script.InitializeFromFile(@"..\..\..\Scripts\script1.py"); // no params call Console.WriteLine(script.Execute<string>("script1class", "Hello")); // single param call Console.WriteLine(script.Execute<string>("script1class", "HelloWithName", "Paulo")); // pass instance of complex type var person = new Person { Name = "Paulo Mouat", Address = "Boston" }; Console.WriteLine(script.Execute<string>("script1class", "HelloWithPerson", person)); // duck typing var hasNameLikePerson = new HasNameLikePerson { Name = "HAL", Model = 9000 }; Console.WriteLine(script.Execute<string>("script1class", "HelloWithPerson", hasNameLikePerson)); // change field in passed type var changeState = new Person { Name = "Paulo Mouat", Address = "Boston" }; Console.WriteLine("Original state: " + changeState); script.Execute<string>("script1class", "ChangeAddress", changeState); Console.WriteLine("New state: " + changeState); Console.WriteLine(); }