前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c#(IronPython)调用Pyth

c#(IronPython)调用Pyth

作者头像
py3study
发布2020-01-19 17:22:25
9990
发布2020-01-19 17:22:25
举报
文章被收录于专栏:python3python3python3

直接一段代码演示

 public void StartTCP()
        {
            ScriptEngine engine = Python.CreateEngine();
            var paths = engine.GetSearchPaths();
            List<string> lstPath = new List<string>();
            lstPath.AddRange(paths);
            lstPath.Add("Script");
           // lstPath.Add(@"D:\Program Files\Python\Python37\Lib");
            lstPath.Add(@"D:\Program Files\IronPython 2.7\Lib");
            engine.SetSearchPaths(lstPath.ToArray());
            var scope = engine.CreateScope();
            var source = engine.CreateScriptSourceFromFile("Script/TCPClsClient.py");
             dynamic result= source.Execute(scope);
            //调用函数的2种方法
            // 第一种,通过参数方式转换委托调用,看起来不太简洁
            var  SetAddress = scope.GetVariable<Action<string,int>>("SetAddress");
            var Con = scope.GetVariable<Action>("Connect");
            var SendData = scope.GetVariable<Action<string>>("Send");
            var Revcive = scope.GetVariable<Action>("Revcive");
            var Close = scope.GetVariable<Action>("Close");
            SetAddress("localhost", 7777);
            Con();
            SendData("jinyu");
            Revcive();
            Close();
            //第二种,没有智能化提示,必须要转换为dynamic
            result = scope;
            result.SetAddress(result, "localhost", 7777);
            result.Connect();
            result.Send("jinyu");
            result.Recvice();
            result.Close();
            //因为是2.7版本,所以不能支持3.X版本的类方法调用
            //例如: result= scope.GetVariable("TCPClsClient");//调用构造,返回实例
            //result.SetAddress(result, "localhost", 7777);//不能执行,版本不支持3.X类函数调用
        }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-05-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档