c#隐式调用python_C#调用python脚本样例

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

按 Ctrl+C 复制代码using System;

using IronPython.Hosting;

using Microsoft.Scripting.Hosting;

namespace ConsoleApp1

{

///

/// 测试对象.

///

/// 用于传递数据给 Python 脚本

///

public class TestDataObject

{

///

/// 用户名.

///

public string UserName { set; get; }

///

/// 年龄.

///

public int Age { set; get; }

///

/// 描述信息.

///

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 = 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();

}

}

}

//– 运行结果

//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;按 Ctrl+C 复制代码

转载自:https://blog.csdn.net/weixin_42052460/article/details/80895432

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值