Interaction between IronPython and C #

Source: Internet
Author: User

I. Introduction

Python is an object-oriented, literal translation computer programming language. It is also a powerful and complete general-purpose language and has been developed for more than a decade. It is mature and stable. This language is very simple and clear syntax, suitable for completing a variety of high-level tasks, almost all operating systems can run. At present, technologies based on this language are rapidly developing, the number of users is rapidly expanding, and there are a lot of related resources. IronPython was developed by Jim Hugunin at Microsoft. the Python implementation on the. NET platform includes the complete compiler, execution engine, and runtime support.. NET. Microsoft is very concerned about IronPython and dynamic languages of the. NET framework and has provided IronPython support in various projects. IronPython has been well integrated into. NET framework. Strings in Python correspond to. NET string objects, and corresponding methods in Python are also provided in IronPython. The same applies to other data types.

Author

Refer:

Learn about DLR: http://rednaxelafx.javaeye.com/blog/241430

Jim's blog: http://blogs.msdn.com/hugunin/default.aspx

Http://developer.51cto.com/art/200910/156377.htm

Http://www.msuniversity.edu.cn/m_LearnCenterInfo/Detail.aspx? Id = 102

Ii. Basic Knowledge

1. Installation

Since I am currently using vs2008 so to http://ironpython.codeplex.com/Release/ProjectReleases.aspx? ReleaseId = 12482 download 2.6 official version, I under the IronPython-2.6.msi, directly install on the line. The default installation path should be C: \ Program Files \ IronPython 2.6

2. Introduce the corresponding dll

Create a console application and reference IronPython. dll, Microsoft. Scripting. Core. dll, and Microsoft. Scripting. dll in C: \ Program Files \ IronPython 2.6.

3. Applications

C: \ Program Files \ IronPython 2.6 \ Tutorial \ Tutorial.htm is the IronPython application guide, which is carefully written. Ipy.exe is the running console of IronPython. You can use this tool to learn IronPython syntax. IronPython syntax is not detailed here. If you want to learn more, you can download IronPython in Action.

Iii. Interaction between IronPython and C #

1. C # Use IronPython code

We want to directly run the code in IronPython in C #, for example, the result value of 1 + 2.

 

Code

ScriptEngine engine = Python. CreateEngine ();
ScriptScope scope = engine. createlist ();
Var strExpression = "1 + 2 ";
Var sourceCode = engine. CreateScriptSourceFromString (strExpression );
Var actual = sourceCode. Execute <int> ();
Console. WriteLine (actual );

 

 

Execution result:

3

ScriptEngine and ScriptScope are two basic classes for using IronPython scripts in. net. ScriptSource is the basic class for running IronPython, and sourceCode is a ScriptSource.

Sometimes we want to input a variable value to IronPython code.

 

Code

ScriptEngine engine = Python. CreateEngine ();
ScriptScope scope = engine. createlist ();
Var strExpression = "\" Hello: \ "+ str ";
Var sourceCode = engine. CreateScriptSourceFromString (strExpression );
Scope. SetVariable ("str", "Python ");
Var actual = sourceCode. Execute <string> (scope );
Scope. RemoveVariable ("str ");
Console. WriteLine (actual );

 

Execution result:

Hello: Python

2. C # Call the IronPython Function

Call the MyFunction function in IronPython

 

Code

Var strExpression = @"
Def MyFunction (n ):
Return 2 * n
";
Var sourceCode = engine. CreateScriptSourceFromString (strExpression). Compile (). Execute (scope );

Var func = engine. GetVariable <Func <int, int> (scope, "MyFunction ");

Console. WriteLine (func (3 ));

 

Note def MyFunction (n): there must be no spaces before, and return 2 * n must have spaces.

3. IronPython calls C # Functions

Call an existing function in C # In IronPython

 

 

Code

Static void Main (string [] args)
{
ScriptEngine engine = Python. CreateEngine ();
ScriptScope scope = engine. createlist ();
Var strExpression = "CMethod ('python ')";
Var sourceCode = engine. CreateScriptSourceFromString (strExpression );
Scope. SetVariable ("CMethod", (Func <string, string>) TMethod );
Var actual = sourceCode. Execute <string> (scope );
Scope. RemoveVariable ("CMethod ");
Console. WriteLine (actual );
}
Public static string TMethod (string info)
{
Return "Hello:" + info;
}

 

To use a function in an object

 

Code

Test test = new Test ();
Var strExpression = @"
Test. Hello ()
";
Var sourceCode = engine. CreateScriptSourceFromString (strExpression );
Scope. SetVariable ("test", test );
Var actual = sourceCode. Execute <string> (scope );
Console. WriteLine (actual );

 

If you need to use an object in IronPython instantiation, it is a little complicated. Here we have created an IronPythonTest assembly, and we want to use the Test class in IronPythonTest assembly in IronPython Code. The Code is as follows:

 

Code

Var strExpression = @"
Import clr, sys
Clr. AddReference ('ronpythontest ')
From IronPythonTest import *
Test = Test ()
Test. Hello ()
";
Var sourceCode = engine. CreateScriptSourceFromString (strExpression );
Var actual = sourceCode. Execute <string> (scope );
Console. WriteLine (actual );

 

Test code:

Namespace IronPythonTest
{
Public class Test
{
Public string Hello ()
{
Return "Hello World ";
}
}
}

Clr. AddReference ('ironpythontest') is used to add an assembly

From IronPythonTest import * is used to add namespaces.

Refer:

Http://www.236z.com/html/30/6/9/2009/11/10/67471.html

Comparison between IronPython and C # execution speed

 

 

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.