Executing a script alone is of little significance, and the most important thing is that the embedded script interacts with the host language, and I'm going to talk about it here.
The best part of the IronPython script is almost seamless integration with C # (or the. NET Framework set), and the classes implemented by C # can easily be passed into the script. (Currently, unfortunately, the IronPython script has not yet supported the embedded version, hoping to implement the embedded version in the near future).
This is the code for the host language: public partial class Form1:form
... {
Public Form1 ()
... {
InitializeComponent ();
}
private void Button1_Click (object sender, EventArgs e)
... {
Pythonengine scriptengine = new Pythonengine ();
TestData td = New TestData ();
Td.intnum = 10;
Scriptengine.defaultmodule.globals["TD"] = TD;
Scriptengine.execute (TextBox1.Text);
Shows whether Intnum is recognized and changed by script
MessageBox.Show (Td.intNum.ToString ());
}
}
Data interaction Classes
public class TestData
... {
public int intnum = 0;
}
The above key code is: scriptengine.defaultmodule.globals["TD"] = TD;
Scriptengine.defaultmodule This is a dictionary class that can be added to several different types for scripting use. The keyword in the dictionary is consistent with the name of the object in the script (the content is arbitrary, it can be different from the name of the object you are sending, the key depends on your intention).
The script operates on a variable in the class and then displays the result (the corresponding script is in the text box below).
How, this is also very simple. In the next article, I'll talk about how the IronPython script invokes the DLL library of the C # implementation.