# -*- 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


 
using System; 

using IronPython.Hosting;
using Microsoft.Scripting.Hosting; namespace ConsoleApp1
{ /// <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);
} }
public class RunPython
{
public void RunPythonTest()
{
// 加载外部 python 脚本文件.
ScriptRuntime pyRumTime = Python.CreateRuntime();
dynamic obj = pyRumTime.UseFile("TestPythonFile.py"); // ==================================================
// 简单调用脚本文件中的方法.
Console.WriteLine(obj.welcome("Test C# Call Python."));
Console.WriteLine(obj.welcome("测试中文看看是否正常!")); // ==================================================
// 测试自定义对象.
TestDataObject testObj = new TestDataObject()
{
UserName = "张三",
Age = ,
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();
}
} } //-- 运行结果
//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;

Python实例--C#执行Python脚本,传参的更多相关文章

  1. expect脚本远程登录、远程执行命令和脚本传参简单用法

    expect介绍: 最近想写一个自动化安装脚本,涉及到远程登录.分发文件包.远程执行命令等,其中少不了来回输入登录密码,交互式输入命令等,这样就大大降低了效率,那么有什么方法能解决呢?不妨试试expe ...

  2. npm脚本传参问题

    npm脚本传参问题(比如设置env参数) windows环境下: "build": "set NODE_ENV=dev&& gulp", &qu ...

  3. C# 服务里面调用Python.exe 来执行python文件

    问题描述:在WCF服务里面通过调用python.exe来执行py文件,像下面这样py文件路径+参数,用空格隔开.会出现调用结果为空的现象 System.Diagnostics.ProcessStart ...

  4. python--脚本传参与shell脚本传参(位置参数)

    写一个最简单的shell脚本,了解shell脚本是如何传参 1. vim test1.sh name=$1 age=$2 echo ${name} echo ${age} 2.调用脚本并传参 sh t ...

  5. shell 脚本传参

    在 shell 中我们会见到  $0.$1.$2这样的符号,这是什么意思呢? 简单来说 $0 就是你写的shell脚本本身的名字,$1 是你给你写的shell脚本传的第一个参数,$2 是你给你写的sh ...

  6. python 执行文件时传参

    ## test.py ## ####################### import sys if __name__ == "__main__": args = sys.arg ...

  7. Python编程系列---使用装饰器传参+字典实现动态路由

    # 实现一个空路由表,利用装饰器将url和功能函数的对应关系自动存到这个字典中 router_dict = {} # 定义一个装饰器 # 再给一层函数定义,用来传入一个参数,这个参数就是访问的页面地址 ...

  8. Python利用ctypes实现按引用传参

    C的代码 void test_cref(char *a, int *b, char *data) { , sizeof(char)); strcpy(p, "cute"); a[] ...

  9. shell脚本传参执行spark-submit

    读取多个目录下的本地文件,多个目录通过循环遍历的方式,以参数传递: #!/bin/bash i=0while [ $i -lt 10000 ] do echo "i=$i" spa ...

随机推荐

  1. SQL传入时间获取到时间的周一和周日

    declare @time datetime declare @timeMonday datetime set @time='2013-11-07' ) ,@time) select @timeMon ...

  2. Objective-C设计模式——适配器Adapter(接口适配)

    适配器模式 适配器模式通俗来讲,其实就是对客户端添加新的类但却不修改客户端和新的类的接口.此时我们需要自己来实现适配,在适配器模式中有Target对象,即客户端所需要的接口对象,Adaptee对象,即 ...

  3. jquery滚轮事件

    // jquery 兼容的滚轮事件 $(document).on("mousewheel DOMMouseScroll", function (e) { var delta = ( ...

  4. 在中间层 .NET 应用程序中通过授权管理器使用基于角色的安全

    基于角色的安全是从 Windows NT 的第一个版本开始在 Windows 平台上发展而来的.使用角色,操作系统可以通过检查称为 BUILTIN\Administrators 的组的安全上下文做出一 ...

  5. 初始MongoDB------将MongoDB创建为Windows服务

    上一遍我写的是关于基本的MongoDB的安装,可能不是很详细,也写得很不好,不过这次我们会详细的说说,如果将MongoDB部署在你的Windows电脑上. 1.配置环境变量 如果每次都要在CMD进入M ...

  6. mongo 3.4分片集群系列之八:分片管理

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  7. Linux基础之操作系统

    一.什么是操作系统 简单来说,操作系统就是一个协调.管理和控制计算机硬件资源和软件资源的控制程序. 二.操作系统存在的意义 究根结底,我们日常对计算机的管理是对计算机硬件的管理.经过近百年的时间,现代 ...

  8. spring 实例 bean 的方式

    一.使用构造器实例化: <bean id="personService" class="cn.mytest.service.impl.PersonServiceBe ...

  9. UEditer的使用

    1.首先到官网下载http://ueditor.baidu.com/website/download.html#ueditor 2.然后把解压后的文件复制到项目中(放在UEditer中),如图 3.在 ...

  10. 梦想MxWeb3D协同设计平台 2018.10.12更新

    SDK开发包下载地址: http://www.mxdraw.com/ndetail_10107.html 1. 全新的在线的三维协同设计平台,高效异步方式,基于JavaScript和WebGL技术,前 ...