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'

其他例子

待补充

相关推荐
知秋正在9967 分钟前
Java实现Html保存为.mhtml文件
java·开发语言·html
我是小疯子6611 分钟前
VSCode远程Python开发:保姆级SSH教程
python
q***441518 分钟前
Java性能优化实战技术文章大纲Java性能优化的核心目标与原则
java·开发语言·性能优化
csbysj202021 分钟前
Ruby CGI Session
开发语言
OliverZhao24 分钟前
iPhoto:基于 Python + PySide6 的高性能 macOS 风格照片管理器
python
SoRound25 分钟前
【Shopee Games AI 模型使用经验】年度总结之 ------ 识别人脸特征,生成动漫形象
python·openai
郝学胜-神的一滴28 分钟前
机器学习特征预处理:缺失值处理全攻略
人工智能·python·程序人生·机器学习·性能优化·sklearn
lly20240628 分钟前
NumPy 迭代数组
开发语言
rgeshfgreh28 分钟前
Python闭包:函数记住状态的秘密
开发语言·python
古城小栈31 分钟前
Cargo命令工具
开发语言·rust