C # Example of data interaction with IronPython scripts

Source: Internet
Author: User

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.

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.