IronPython是一种在 .NET及 Mono上的 Python实现,由微软的 Jim Hugunin所发起,是一个开源的项目,基于微软的 DLR引擎。IronPython的在CodePlex上的主页:http://www.easck.com/
使用场景:
如果你的小伙伴会写Python脚本,而且已经实现大部分项目的功能不需要再用C# 实现。现在缺少窗体,此时Python+C#的组合就可以完美的结局问题啦!
示例:
借由IronPython,就可以利用.NET执行存储在Python脚本中的代码段。下面通过简单的示例说明如何应用C#调用Python脚本。
1、在VS中新建窗体项目:IronPythonDemo
2、VS的菜单中打开“Nuget程序包管理器”

3、搜索IronPython程序包并安装

4、在exe程序所在文件夹下(此例中为".IronPythonDemoIronPythonDemobinDebug"),创建Python脚本。或将现有的脚本拷贝到该目录下。Python示例脚本实现求两个数的四则运算:
num1=arg1
num2=arg2
op=arg3
if op==1:
result=num1+num2
elif op==2:
result=num1-num2
elif op==3:
result=num1*num2
else:
result=num1*1.0/num2
5、修改工程的配置文件App.config如下:
其中microsoft.scripting节点中设置了IronPython语言引擎的几个属性。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="microsoft.scripting" type="Microsoft.Scripting.Hosting.Configuration.Section, Microsoft.Scripting"/>
</configSections>
<microsoft.scripting>
<languages>
<language names="IronPython;Python;py" extensions=".py" displayName="Python" type="IronPython.Runtime.PythonContext, IronPython"/>
</languages>
</microsoft.scripting>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>










