Embedding IronPython interactions in. Net

Source: Internet
Author: User
Tags object execution instance method net variables return
With the release of Ironpyhon 2.0, the. NET dynamic Language Runtime is also more mature, and in 2.0 we can write various logical units in the architecture system in the form of adhesives, which are easy to modify and adaptable to changeable business scenarios. Of course, my goal is to embed the scripting engine in the Platform Framework, rather than using Ipy.exe to perform a "standalone" task.

For a. NET project to actually interact with a script, you need to provide a script engine implementation. This must refer to some basic concepts of the DLR.

The following figure is taken from the DLR Help file, which allows us to understand the composition of the DLR based on the basics.

The following figure describes the basic execution process for the DLR.

Scriptruntime: Create the DLR runtime environment, which is the starting point for the entire execution process, which represents a global execution state (such as an assembly reference, and so on). Multiple scriptruntime can be started in each application domain (AppDomain).

Scriptscope: Builds an execution context in which the environment and global variables are saved. Host (host) can provide an execution context for multiple data isolation by creating different scriptscope.

SCRIPTENGINE:DLR dynamic languages, such as IronPython, perform classes, which can be used to parse and execute dynamic language codes.

Scriptsource: Manipulating the type of dynamic language code, we can compile (Compile), read (read Code Lines), or run (Execute) code.

Compiledcode: Call Scriptsource.compile () to compile the source code into Compiledcode so that multiple executions do not need to be compiled repeatedly to improve performance.

Objectoperations: Provides a method that allows us to manipulate DLR Object Members (member) in the host (host).

Now let's build the scripting engine.

Of course we have to join the relevant references provided by Ironpyhton and import namespaces

Using Ironpython.hosting;

Using Ironpython.compiler;

Using Ironpython.runtime;

Using Microsoft.scripting;

Using System.Runtime.Remoting;

1, Hello World

The following are the referenced contents:
var py = @ "
def test ():
Return ' Hello world~! ';
Print test (); ";
var engine = Python.createengine ();
var code = engine. Createscriptsourcefromstring (py, sourcecodekind.statements);
Code. Execute ();

Output: Hello world~!

Note: Python has strict requirements for the format of the source code indentation.

2. Provide variables to the Python context

var scope = engine. Runtime.globals; Engine. Runtime.createscope ();

Scope. SetVariable ("X", 123);

3. Read the variables in the python context

var x = scope. Getvariable<int> ("X");

You can see that scriptscope can pass data between Host and Scriptruntime.

4. Object instance Sharing

The following are the referenced contents:
var py = @ "
o.x = o.x + 2;
print o.x; ";
var engine = Python.createengine ();
var code = engine. Createscriptsourcefromstring (py, sourcecodekind.statements);
var scope = engine. Runtime.globals;
var o = new Data {X = 123};
Scope. SetVariable ("O", O);
Code. Execute (scope);
Console.WriteLine (o.x);

Loading an assembly and managed by Scriptruntime

5. Create ASSEMBLY Test.dll

The following are the referenced contents:
Namespace My.library
{
public class MyClass
{
public int Test (int x)
{
return ++x;
}
}
}

Create host Program

The following are the referenced contents:
var py = @ "
Import CLR;
from my.library import MyClass;
from System import Console;
o = MyClass ();
x.x = O.test (x.x);
Console.WriteLine (x.x); ";
var engine = Python.createengine ();
Engine. Runtime.loadassembly (assembly.getassembly (typeof (int))); Mscorlib.dll
Engine. Runtime.loadassembly (Assembly.LoadFrom ("Test.dll")); Test.dll
var code = engine. Createscriptsourcefromstring (py, sourcecodekind.statements);
var scope = engine. Runtime.globals;
var x = new Data {x = 123};
Scope. SetVariable ("x", x);
Code. Execute (scope);
Console.WriteLine (x.x);

Read Python context object instance properties

The following are the referenced contents:
var py = @ "
Class Class1:
def __init__ (self):
SELF.I = 100
Def Inc (self):
self.i=self.i+100
o = Class1 () ";
var o = scope. GetVariable ("O");
var i = engine. Operations.getmember<int> (O, "I");

Read Python Context object instance method

Engine. Execute ("O.inc ()", scope); This object method has already been executed in the context environment



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.