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
{
    /// <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 = 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

You may also like...