Python中调用C#的dll库
Python中调用C#的dll库
1、创建C#的dll库。dll名称为MyCsharpDLL.dll,下面是dll里面的代码
using System; namespace MyCsharpDLL { public class Test { public void Print() { Console.WriteLine("Hello world!!!"); } public void Print(string msg) { Console.WriteLine($"Hello {msg}!!!"); } public double Add(double x, double y) { return x + y; } } }
2、通过在Python中调用clr(pythonnet中工具)来对C#的dll库进行加载使用,下面是Python调用C#里面方法的代码
import os import clr import sys sys.path.append(os.getcwd()) # 读取DLL文件 clr.FindAssembly("MyCsharpDLL.dll") dll = clr.AddReference("MyCsharpDLL") print(dll) from MyCsharpDLL import * def print_hi(name): # 实例化类 instance = Test() # 无输入及无返回 instance.Print() # 有输入及无返回 instance.Print("lqwvje") # 有输入及输出 add_data = instance.Add(1, 1) print(add_data) # 按间距中的绿色按钮以运行脚本。 if __name__ == '__main__': print_hi('PyCharm')
合集:
python
分类:
编程相关 / Python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了