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'

其他例子

待补充

相关推荐
future14121 小时前
C#学习日记
开发语言·学习·c#
菜包eo1 小时前
二维码驱动的独立站视频集成方案
网络·python·音视频
Yo_Becky1 小时前
【PyTorch】PyTorch预训练模型缓存位置迁移,也可拓展应用于其他文件的迁移
人工智能·pytorch·经验分享·笔记·python·程序人生·其他
king_harry1 小时前
Java程序-OceanBase Connector/J 示例
开发语言
yzx9910131 小时前
关于网络协议
网络·人工智能·python·网络协议
fangeqin1 小时前
ubuntu源码安装python3.13遇到Could not build the ssl module!解决方法
linux·python·ubuntu·openssl
傻啦嘿哟2 小时前
Python 办公实战:用 python-docx 自动生成 Word 文档
开发语言·c#
翻滚吧键盘2 小时前
js代码09
开发语言·javascript·ecmascript
Jay Kay2 小时前
TensorFlow源码深度阅读指南
人工智能·python·tensorflow
q567315232 小时前
R语言初学者爬虫简单模板
开发语言·爬虫·r语言·iphone