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'

其他例子

待补充

相关推荐
知识分享小能手17 分钟前
Flask入门学习教程,从入门到精通,Flask智能租房——前期准备 知识点详解(5)
python·学习·flask
Curvatureflight20 分钟前
【架构实战】生产级大模型 API 接入指南:流式响应(Streaming)异常处理与监控闭环
python·架构
smj2302_796826521 小时前
解决leetcode第3943题递增后的数对数量
数据结构·python·算法·leetcode
এ慕ོ冬℘゜1 小时前
JS 前端基础面试题
开发语言·前端·javascript
浩少7021 小时前
【无标题】
java·开发语言
deepin_sir1 小时前
06 Chroma_持久化与生产环境部署实战
python
nnsix2 小时前
C# 字符串 根据换行符分割
开发语言·c#
Vallelonga2 小时前
Rust Conversion 工具 trait AsRef AsMut
开发语言·rust
Vallelonga2 小时前
Rust 中的“解引用”和智能指针与 MutexGuard 等
开发语言·rust
小鱼仙官2 小时前
Windonws 视频存储,10s/不限时
开发语言·qt·音视频