Loryan55
Loryan55

Reputation: 325

How to load IronPython script with C#, with adding variables to scope before execute it

I have following code:

m_pyScope = pyRuntime.UseFile(filename);

To load script file within IronPython runtime in C#.

The problem is that i need to register my custom functions, variables, etc to that scope, BEFORE executing this script. Code which i use now executes script body after compile.

Is there a way to make loading IronPython script like this:

  1. Compile from file or text variable
  2. Create empty scope (from Engine's createScope())
  3. Do something with that scope, for example add some function
  4. Execute script body, e.g. code which is outside any function or class

Upvotes: 1

Views: 4424

Answers (1)

Loryan55
Loryan55

Reputation: 325

I found the solution. The best way to load script is in answer to this question:

IronPython integration in C#: a specific problem/question

Create engine from runtime, then scope, then compile source. After that you can call source.Execute(scope) to execute script in scope created before

Upvotes: 4

Related Questions