Preface
IronPython is a Python implementation on. NET and Mono, launched by Microsoft's Jim Hugunin (also the Jython creator) and is an open source project based on the Microsoft DLR engine. In 2007, developers decided to rewrite the architecture and use the dynamic type system to make it easy for more scripting languages to be ported to the NET Framework. IronPython's official did not implement the Python Universal class Library, only implemented some core classes, the community's Open source class library implementation has:
Fepy (http://fepy.sourceforge.net/): Fepy provides the implementation of most common class libraries for Python for IronPython.
Test for IronPython
(1) Create a new form project in VS2017: Ironpythontest.
(2) The "NuGet Package Manager" opens in the VS menu bar tool:
(3) Search for the IronPython package and install:
(4) After the installation succeeds, the Python script can be created under the folder where the EXE program is located (or in other directories, by specifying a relative path):
Example (arithmetic for two numbers)
Num1=arg1 num2=arg2 op=arg3 if op==1: result=num1+num2 elif op==2: result=num1-num2 Elif op==3: result=num1*num2 Else: result=num1*1.0/num2
(5) Modify the configuration file for the Project App. Config:
(6) Calculate the button's Click event:
private void Btncalculate_click (object sender, EventArgs e) { Scriptruntime scriptruntime = Scriptruntime.createfromconfiguration (); ScriptEngine Rbeng = scriptruntime.getengine ("python"); Scriptsource Source = Rbeng.createscriptsourcefromfile ("hello.py");//Set Script file scriptscope scope = Rbeng.createscope (); Try { //Set parameter scope. SetVariable ("Arg1", Convert.ToInt32 (Txtnum1.text)); Scope. SetVariable ("Arg2", Convert.ToInt32 (Txtnum2.text)); Scope. SetVariable ("Arg3", operation. SelectedIndex + 1); } catch (Exception) { MessageBox.Show ("Input is incorrect. "); } Source. Execute (scope); Labelresult.text = scope. GetVariable ("result"). ToString (); }
(7) Operation Result:
Refer to related articles:
Using IronPython to integrate Python and. NET
Cross-language and cross-compiler of those pits (CPython vs IronPython)
C#NOTE13: How to call Python in C #