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'

其他例子

待补充

相关推荐
adfass21 小时前
桌面挂件时钟/多功能时钟C++
开发语言·c++·算法
Rust语言中文社区1 天前
【Rust日报】 walrus:分布式消息流平台,比 Kafka 快
开发语言·分布式·后端·rust·kafka
笨笨没好名字1 天前
AI4CAD:用python+creo实现批量参数建模(creo二次开发示例)
数据库·python·microsoft
多多*1 天前
Threadlocal深度解析 为什么key是弱引用 value是强引用
java·开发语言·网络·jvm·网络协议·tcp/ip·mybatis
Python×CATIA工业智造1 天前
Python多进程爬虫实战:豆瓣读书数据采集与法律合规指南
开发语言·爬虫·python
java1234_小锋1 天前
基于Python深度学习的车辆车牌识别系统(PyTorch2卷积神经网络CNN+OpenCV4实现)视频教程 - 集成到web系统-识别历史记录实现
python·深度学习·cnn·车牌识别
一只乔哇噻1 天前
java后端工程师+AI大模型进修ing(研一版‖day56)
java·开发语言·学习·算法·语言模型
美团测试工程师1 天前
软件测试面试题2025年末总结
开发语言·python·测试工具
B站计算机毕业设计之家1 天前
Python+Flask 电商数据分析系统(Selenium爬虫+多元线性回归)商品数据采集分析可视化系统 实时监控 淘宝数据采集 大屏可视化 (附源码)✅
大数据·爬虫·python·selenium·机器学习·flask·线性回归
F***74171 天前
PHP操作redis
开发语言·redis·php