C # embedding IronPython script sample (Hello World)

Source: Internet
Author: User

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.

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.