As the project gradually ended and the IronPython script became more and more familiar, here is the first: The simplest Hello World program for a friend who is interested in IronPython scripts but doesn't get started.
First of all, we must have a IronPython script engine library (IronPython.dll), I use the version is V1.0, you can directly down to the relevant source on the Internet, compiled that generates IronPython.dll.
After you create a new C # Desktop program that references the library, we start writing the first program.
Here is the code in C #:
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using Ironpython.hosting;
Namespace Testironpython
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}
private void Button1_Click (object sender, EventArgs e)
{
Pythonengine scriptengine = new Pythonengine ();
Scriptengine.execute (TextBox1.Text);
}
}
}
The code is simple, declares a ScriptEngine instance, executes the code directly with execute. Let's look at how the PY code should be written:
Import CLR
Clr. Addreferencebypartialname ("System.Windows.Forms")
Clr. Addreferencebypartialname ("System.Drawing")
From System.Windows.Forms Import *
From System.Drawing Import *
MessageBox.Show ("Hello world!")
The first line of code is important to import the. NET CLR, Using the CLR's Addreferencebypartialname method to load our familiar System.Windows.Forms and system.drawing libraries, you can finally execute the MessageBox method in. NET directly.
After running, click Button1 directly to pop up a dialog box "Hello world!"
How, is not very simple.