ironphy  microsoft.scripting dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using Common;

namespace PyConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //ScriptRuntime pyRuntime = Python.CreateRuntime();
            //dynamic py = pyRuntime.UseFile(@"E:\Test\test.py");
            var engine = Python.CreateEngine();
            var scope = engine.CreateScope();
            var source = engine.CreateScriptSourceFromFile(@"E:\Test\test.py");
            source.Execute(scope);
            var say_hello = scope.GetVariable<Func<object>>("say_hello");
            say_hello();
            var get_text = scope.GetVariable<Func<object>>("get_text");
            var text = get_text().ToString();
            Console.WriteLine(text);
            var add = scope.GetVariable<Func<object, object,object>>("add");
            var name = scope.GetVariable<Func<object,object>>("get_name");
            int[] ints = { 1, 2, 3, 4 };
            List<int> list = new List<int>();
            Dictionary<int, int> dic = new Dictionary<int, int>();
            int i = 0;
            SimpleTest test = new SimpleTest();
            var result1 = name(test);  Python调用C#
            list.AddRange(ints);
            var result2 = add(list, i);  传递参数并返回值
            Console.WriteLine(result2);
            
            //var result1 = add("hello", i.ToString());
            //Console.WriteLine(result1);
            Console.Read();
        }
    }
}

以下C#代码测试Python调用C#时使用

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

namespace Common
{
    public class SimpleTest
    {
        public string GetName()
        {
            return "C#";
        }
    }
}

Python代码

import clr
import sys
sys.path.append(r'E:\Test\PyConsoleTest\PyConsoleTest\bin\Debug')
clr.AddReferenceToFile("Common.dll")
from Common import *
def say_hello():
    print "hello!"

def get_text():
    return "text from hello.py"

def add(arg1,arg2):  传递参数返回参数
    #var y
    for x in arg1:
        arg2 += x
    return arg2
    #print y
    #return arg1+arg2;
def get_name(SimpleTest): 调用方法返回
    return SimpleTest.GetName()

PS:如果你的Python没有设置encoding 删除所有中文即可使用

最新文章

  1. neutron debug
  2. js 事件冒泡是什么如何用jquery阻止事件冒泡
  3. ubuntu1404服务器版中设置root用户
  4. MyBatis无法根据中文条件查询出结果
  5. A Knight&#39;s Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏
  6. SQL VS NoSQL 如何选择数据库
  7. 关于IOS中UIWebView 加载HTML内容
  8. PhotoShop 移动工具详解
  9. Android 导入第三方jar时 出现ClassNotFoundException
  10. Jquery一些实用函数
  11. 一文了解Python中的判断语句
  12. Qt 编程中 namespace Ui { class Widget; } 解析
  13. Hexo NexT 博客本地搭建指南
  14. 【1】ASP.NET异步(1)
  15. Docker容器学习与分享08
  16. jenkins之参数化构建
  17. linux下去掉pdf的密码(前提:知道密码)
  18. Memcached理解笔2---XMemcached&amp;Spring集成
  19. sql count执行速度测试
  20. 【bzoj1833】 ZJOI2010—count 数字计数

热门文章

  1. TLSv网络安全标准,会话加密协议展望未来
  2. JVM、JRE、JDK的区别
  3. &quot;||&quot; 在sql中有什么用
  4. Java programming language does not use call by reference for objects!
  5. head、tail 命令和实时(real-time)更新
  6. logback为单独的包或者类配置输出文件
  7. 【leetcode】1021. Best Sightseeing Pair
  8. SpringMVC的 几个注解
  9. MVVM MVC
  10. 4412 GPIO初始化