01-08-2021, 07:29 AM
Also you can add py files to script assembly resources. In Properties dialog.
C# code:
// script "ipy_code3.cs" /*/ r Libraries\IronPython.dll; r Libraries\Microsoft.Scripting.dll; r Libraries\Microsoft.Dynamic.dll; resource script.py /string; /*/ //.
using Au; using System; using Microsoft.Scripting.Hosting;
using IronPython.Hosting;
using System.Collections.Generic;
using System.Linq;
partial class Script : AScript { [STAThread] static void Main(string[] a) => new Script(a); Script(string[] args) { //;;;
AOutput.RedirectConsoleOutput=true;
ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromString(Au.Util.AResources.GetString("script.py")); //or "subfolder/script.py"
ScriptScope scope = engine.CreateScope();
ObjectOperations op = engine.Operations;
source.Execute(scope); // class object created
object classObject = scope.GetVariable("MyClass"); // get the class object
object instance = op.Invoke(classObject); // create the instance
object method = op.GetMember(instance, "myMethod"); // get a method
List<string> result = ((IList<object>)op.Invoke(method)).Cast<string>().ToList();
AOutput.Write(result);
}
}