C#嵌入IronPython脚本示例(hello world)
来源:互联网 发布:json格式怎么写 编辑:程序博客网 时间:2024/06/01 07:28
随着项目的逐渐收尾, 对IronPython脚本也越来越熟悉,这里为IronPython脚本感兴趣但不入门的朋友写几篇使用心得,这是第一个:最简单的hello world程序。
首先,我们必须有一个IronPython脚本引擎库(IronPython.dll),我用的版本是V1.0,你可以在网上直接下到相关源码,编译后即生成IronPython.dll。
新建一个C#桌面程序,引用该库后,我们便开始编写第一个程序。
下面是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);
}
}
}
代码很简单,声明了一个scriptEngine 实例,直接用Execute执行代码即可。下面看看py的代码该怎么写:
import clr
clr.AddReferenceByPartialName("System.Windows.Forms")
clr.AddReferenceByPartialName("System.Drawing")
from System.Windows.Forms import *
from System.Drawing import *
MessageBox.Show("Hello World!")
第一句代码很重要,导入.net clr,用clr的AddReferenceByPartialName方法加载我们熟悉的System.Windows.Forms和System.Drawing库,最后可以直接执行.net中的MessageBox方法。
运行后,直接单击button1,即可弹出一个对话框"Hello World!"
怎么样,是不是很简单?!
- C#嵌入IronPython脚本示例(hello world)
- C#嵌入IronPython脚本示例(hello world)
- C#嵌入IronPython脚本示例(hello world)
- C#调用Python脚本的简单示例,IronPython(转载)
- C#与IronPython脚本进行数据交互示例
- C#与IronPython脚本进行数据交互示例
- C#与IronPython脚本进行数据交互示例
- C#与IronPython脚本进行数据交互示例
- 在C#环境中动态调用IronPython脚本(一)
- 在C#环境中动态调用IronPython脚本(二)
- jbpm Hello World 示例
- Hadoop “Hello World” 示例
- RabbitMQ hello world示例
- Hello world ----------AngularJS示例
- koa2 hello world 示例
- IronPython脚本调用C#dll示例
- IronPython脚本调用C#dll示例
- IronPython脚本调用C#dll示例
- 疑惑SCA/SDO vs WCF
- 东拉西扯又一篇
- org.hibernate.exception.SQLGrammarException:Could not execute JDBC batch update问题的解决
- python模块之sqlite数据库
- ASP.NET 1.0 升级至 ASP.NET 2.0问题总结
- C#嵌入IronPython脚本示例(hello world)
- dotnet网上资源
- 最近想了解了解PHP,写点入门感受。
- C#验证Email是否真正存在
- Tags功能的实现
- 新手入门之认识典型Web服务器(图解)
- SD--SD业务的简图
- 基本SQL精选
- Java/JSP中文乱码问题解决心得(转载)