In the following example, I will create a basic python 'plugin' framework in C#.
Python Code
class TestClass(object): def __init__(self): self.A = 5 self.B = 4 def Go(self): return self.A + self.B
C# Code
public static object Execute(string code){ object result;
try { ScriptEngine engine = Python.CreateEngine(); ScriptScope scope = engine.CreateScope(); ScriptSource source = engine.CreateScriptSourceFromString(code); source.Execute(scope); object t = scope.GetVariable("TestClass"); object o = engine.Operations.CreateInstance(t); object r = engine.Operations.InvokeMember(o, "Go"); result = r; } catch (Exception ex) { result = ex; }
return result;}