python-调用c#代码

环境:

win10,net framework 4,python3.9


镜像:

C#-使用IronPython调用python代码_ironpython wpf-CSDN博客

https://blog.csdn.net/pxy7896/article/details/119929434


目录

hello word

不接收参数

hello.cs内容如下:

csharp 复制代码
using System;

class HelloWorld
{
    static void Main()
    {
        Console.WriteLine("hello world");
    }
}

python调用:

python 复制代码
>>> import subprocess
>>> subprocess.call(['C:/Windows/Microsoft.NET/Framework64/v4.0.30319/csc.exe', 'hello.cs'])
Microsoft (R) Visual C# Compiler version 4.8.4084.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240

0
>>> result = subprocess.run(['hello.exe'],capture_output=True,text=True)
>>> result.stdout
'Hello, World!\n'

说明:

  1. subprocess.call后,当前路径会产生一个hello.exe,双击没有反应
  2. subprocess.call的第一个参数可能要根据实际情况修改,此处只是我本机的安装路径,仅作参考
  3. 使用subprocess.run才是真正执行,可以反复run

接收参数

修改C#:

csharp 复制代码
using System;

class HelloWorld
{
    static void Main(string[] args)
    {
        Console.WriteLine(args[0] + " " + args[1]);
    }
}

python调用的时候就需要带参数:(当然要先call制作新的exe)

shell 复制代码
>>> result = subprocess.run(['hello.exe', "hello", "world"],capture_output=True,text=True)
>>> result.stdout
'hello world\n'

其他例子

待补充

相关推荐
黑子哥呢?25 分钟前
安装Bash completion解决tab不能补全问题
开发语言·bash
失败尽常态52328 分钟前
用Python实现Excel数据同步到飞书文档
python·excel·飞书
2501_9044477430 分钟前
OPPO发布新型折叠屏手机 起售价8999
python·智能手机·django·virtualenv·pygame
青龙小码农30 分钟前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿36 分钟前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Leuanghing1 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
彳卸风1 小时前
Unable to parse timestamp value: “20250220135445“, expected format is
开发语言
dorabighead2 小时前
JavaScript 高级程序设计 读书笔记(第三章)
开发语言·javascript·ecmascript
xinxiyinhe2 小时前
如何设置Cursor中.cursorrules文件
人工智能·python
风与沙的较量丶2 小时前
Java中的局部变量和成员变量在内存中的位置
java·开发语言