C#调用python脚本

来源:互联网 发布:战舰世界神风r数据 编辑:程序博客网 时间:2024/06/05 00:29
C# 调用 Python 脚本
操作步骤
1.创建一个控制台程序. O0101_DotNetCallPython.
2.管理 Nuget 程序包,搜索 IronPython , 然后安装。
此操作会自动完成下列操作。
已将引用“IronPython”添加到项目“O0101_DotNetCallPython”
已将引用“IronPython.Modules”添加到项目“O0101_DotNetCallPython”
已将引用“IronPython.SQLite”添加到项目“O0101_DotNetCallPython”
已将引用“IronPython.Wpf”添加到项目“O0101_DotNetCallPython”
已将引用“Microsoft.Dynamic”添加到项目“O0101_DotNetCallPython”
已将引用“Microsoft.Scripting.AspNet”添加到项目“O0101_DotNetCallPython”
已将引用“Microsoft.Scripting”添加到项目“O0101_DotNetCallPython”
已将引用“Microsoft.Scripting.Metadata”添加到项目“O0101_DotNetCallPython”

3.添加Python文件到当前的项目中
创建一个文本文件命名为:hello.py, 
内容如下:
# -*- coding: utf-8 -*-
# 第一行的目的,是为了让代码里面,可以有中文注释信息. (否则要运行报错)
# 这个 Python 脚本, 用于被 C# 来调用.
# 简单测试 Hello World 的效果.
def welcome(name):
return "hello " + name


# 测试 参数为 C# 对象的效果. (获取/设置 C# 对象的属性)
def testAddAge(obj):
obj.Age = obj.Age + 1
obj.Desc = obj.UserName + "又大了一岁 in Python."


# 测试 参数为 C# 对象的效果. (调用 C# 对象的方法)
def testAddAge2(obj):
obj.AddAge(2)


# 测试 List.
def testList(lst):
vResult = ""
for each_item in lst:
vResult = vResult + " " + each_item
return vResult

# 测试 Set.
def testSet(pSet):
vResult = ""
for each_item in pSet:
vResult = vResult + " " + each_item
return vResult


# 测试 Dictionary
def testDictionary(pDictionary):
vResult = ""
for each_item in pDictionary:
vResult = vResult + " " + each_item + "=" + pDictionary[each_item] + ";"
return vResult





把该文件添加的当前的项目中。
在 Visual Studio 中,将该文件包含在项目中
复制到输出目录:如果较新则复制
生成操作:无

4. 编写C# 代码, 调用 hello.py 脚本文件中定义的方法 
内容如下:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using IronPython.Hosting;
using Microsoft.Scripting.Hosting;


namespace O0101_DotNetCallPython
{
class Program
{
static void Main(string[] args)
{
// 加载外部 python 脚本文件.
ScriptRuntime pyRumTime = Python.CreateRuntime();
dynamic obj = pyRumTime.UseFile("hello.py");


// ==================================================
// 简单调用脚本文件中的方法.
Console.WriteLine(obj.welcome("Test C# Call Python."));
Console.WriteLine(obj.welcome("测试中文看看是否正常!"));



// ==================================================
// 测试自定义对象.
TestDataObject testObj = new TestDataObject()
{
UserName = "张三",
Age = 20,
Desc = "",
};
Console.WriteLine("调用脚本前对象数据:{0}", testObj);
obj.testAddAge(testObj);
Console.WriteLine("调用 testAddAge 脚本后,对象数据={0}", testObj);

obj.testAddAge2(testObj);
Console.WriteLine("调用 testAddAge2 脚本后,对象数据={0}", testObj);




// ==================================================
// 测试 List.
IronPython.Runtime.List testList = new IronPython.Runtime.List();
testList.Add("List数据1");
testList.Add("List数据2");
testList.Add("List数据3");
// 测试参数为 List.
string result = obj.testList(testList);
Console.WriteLine("调用 testList , 返回结果:{0}", result);



// ==================================================
// 测试 Set.
IronPython.Runtime.SetCollection testSet = new IronPython.Runtime.SetCollection();
testSet.add("Set数据1");
testSet.add("Set数据2");
testSet.add("Set数据3");

// 测试参数为 Set.
result = obj.testSet(testSet);
Console.WriteLine("调用 testSet , 返回结果:{0}", result);



// ==================================================
// 测试 Dictionary.
IronPython.Runtime.PythonDictionary testDictionary = new IronPython.Runtime.PythonDictionary();
testDictionary["Key1"] = "Value1";
testDictionary["Key2"] = "Value2";
testDictionary["Key3"] = "Value3";
// 测试参数为 Dictionary.
result = obj.testDictionary(testDictionary);
Console.WriteLine("调用 testDictionary , 返回结果:{0}", result);

Console.ReadLine();
}
}



/// <summary>
/// 测试对象.
/// 
/// 用于传递数据给 Python 脚本
/// </summary>
public class TestDataObject
{
/// <summary>
/// 用户名.
/// </summary>
public string UserName { set; get; }

/// <summary>
/// 年龄.
/// </summary>
public int Age { set; get; }


/// <summary>
/// 描述信息.
/// </summary>
public string Desc { set; get; }


public void AddAge(int age)
{
this.Age = this.Age + age;
this.Desc = String.Format("{0}又大了{1}岁 in C#", this.UserName, age);
}


public override string ToString()
{
return String.Format("姓名:{0}; 年龄:{1}; 描述:{2}", this.UserName, this.Age, this.Desc);
}


}


-- 运行结果
hello Test C# Call Python.
hello 测试中文看看是否正常!
调用脚本前对象数据:姓名:张三; 年龄:20; 描述:
调用 testAddAge 脚本后,对象数据=姓名:张三; 年龄:21; 描述:张三又大了一岁 in Py
thon.
调用 testAddAge2 脚本后,对象数据=姓名:张三; 年龄:23; 描述:张三又大了2岁 in C#
调用 testList , 返回结果: List数据1 List数据2 List数据3
调用 testSet , 返回结果: Set数据1 Set数据2 Set数据3
调用 testDictionary , 返回结果: Key3=Value3; Key2=Value2; Key1=Value1;
原创粉丝点击