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'

其他例子

待补充

相关推荐
weixin_408099679 分钟前
2026 图片去水印 API 接口完全指南:一键去除图片水印(附 Python/Java/PHP/C# 示例)
java·python·php·图片处理·api调用·图片去水印·石榴智能
爱喝水的鱼丶23 分钟前
SAP-ABAP:ALV通用封装实践——搭建可复用的ALV开发工具类,减少80%重复代码
开发语言·性能优化·sap·abap·erp·alv
去码头整点薯条ing44 分钟前
某当网登录滑块【协议+OCR】
爬虫·python·ocr
脱胎换骨-军哥1 小时前
C++/Rust无缝互操作:混合系统新常态
开发语言·c++·rust
songroom1 小时前
Kimi K3:Rust封装XTP接口详细教程实践
开发语言·后端·rust
kebeiovo1 小时前
游戏服务端开发:Actor模型详解(Go语言)
开发语言·后端·golang
赶紧写完去睡觉2 小时前
Anaconda 创建虚拟环境与使用
python·pycharm·conda
迷途呀2 小时前
Python:函数中的参数类型
开发语言·笔记·python·langchain·nlp
Herbert_hwt2 小时前
建立Java程序开发
java·开发语言
你驴我2 小时前
WhatsApp 多账号下消息已读回执的实时聚合与推送实践
后端·python