简体   繁体   English

从 python 调用 C# 库

[英]Calling a C# library from python

Anyone can share a working example on how to call a simple C# library (actually its WPF) from python code?任何人都可以分享一个关于如何从 python 代码调用简单的 C# 库(实际上是它的 WPF)的工作示例? (I have tried using IronPython and had too much trouble with unsupported CPython library my python code is using so I thought of trying the other way around and calling my C# code from Python). (我曾尝试使用 IronPython,但我的 python 代码正在使用不受支持的 CPython 库,因此我想尝试另一种方法,并从 Python 调用我的 C# 代码)。

Here is the example I was playing with:这是我正在玩的示例:

 using System.Runtime.InteropServices; using System.EnterpriseServices; namespace DataViewerLibrary { public interface ISimpleProvider { [DispIdAttribute(0)] void Start(); } [ComVisible(true)] [ClassInterface(ClassInterfaceType.None)] public class PlotData: ServicedComponent, ISimpleProvider { public void Start() { Plot plotter = new Plot(); plotter.ShowDialog(); } } }

Plotter is a WPF windows that plots an Ellipse绘图仪是绘制椭圆的 WPF windows

I don't know how to call this code from my python all.我不知道如何从我的 python 中调用此代码。 Any suggestions?有什么建议么?

It is actually pretty easy.这实际上很容易。 Just use NuGet to add the "UnmanagedExports" package to your.Net project.只需使用 NuGet 将“UnmanagedExports”package 添加到 your.Net 项目。 See https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports for details.有关详细信息,请参阅https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports

You can then export directly, without having to do a COM layer.然后您可以直接导出,而无需执行 COM 层。 Here is the sample C# code:这是示例 C# 代码:

 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using RGiesecke.DllExport; class Test { [DllExport("add", CallingConvention = CallingConvention.Cdecl)] public static int TestExport(int left, int right) { return left + right; } }

You can then load the dll and call the exposed methods in Python (works for 2.7)然后,您可以加载 dll 并调用 Python 中的公开方法(适用于 2.7)

 import ctypes a = ctypes.cdll.LoadLibrary(source) a.add(3, 5)

Since your post is tagged IronPython, if you want to use the sample C# the following should work.由于您的帖子被标记为 IronPython,因此如果您想使用示例 C#,以下应该可以工作。

 import clr clr.AddReference('assembly name here') from DataViewerLibrary import PlotData p = PlotData() p.Start()

Python for.Net (pythonnet) may be a reasonable alternative to IronPython in your situation.在您的情况下, Python for.Net (pythonnet)可能是 IronPython 的合理替代方案。 https://github.com/pythonnet/pythonnet/blob/master/README.rst https://github.com/pythonnet/pythonnet/blob/master/README.rst

From the site:从网站:

Note that this package does not implement Python as a first-class CLR language - it does not produce managed code (IL) from Python code.请注意,此 package 并未将 Python 实现为一流的 CLR 语言 - 它不会从 Python 代码生成托管代码 (IL)。 Rather, it is an integration of the CPython engine with the .NET runtime.相反,它是 CPython 引擎与 .NET 运行时的集成。 This approach allows you to use use CLR services and continue to use existing Python code and C-based extensions while maintaining native execution speeds for Python code.这种方法允许您使用 CLR 服务并继续使用现有的 Python 代码和基于 C 的扩展,同时保持 Python 代码的本机执行速度。

Also

Python for .NET uses the PYTHONPATH (sys.path) to look for assemblies to load, in addition to the usual application base and the GAC.除了通常的应用程序库和 GAC 之外,.NET 的 Python 使用 PYTHONPATH (sys.path) 来查找要加载的程序集。 To ensure that you can implicitly import an assembly, put the directory containing the assembly in sys.path.为确保您可以隐式导入程序集,请将包含程序集的目录放在 sys.path 中。

This package still requires that you have a local CPython runtime on your machine.这个 package 仍然要求您的机器上有一个本地 CPython 运行时。 See the full Readme for more info https://github.com/pythonnet/pythonnet有关更多信息,请参阅完整的自述文件https://github.com/pythonnet/pythonnet

This project has been developed for that exact purpose - use C# classes in regular Python该项目已为此目的而开发 - 在常规 Python 中使用 C# 类

https://bitbucket.org/pydotnet/pydotnet/wiki/Home https://bitbucket.org/pydotnet/pydotnet/wiki/Home

All you need to do is to install either MSI or EGG into your CPython.您需要做的就是将 MSI 或 EGG 安装到您的 CPython 中。 PyDotnet is Python module, so the executable stays regular python.exe from your installation of Python or Anaconda. PyDotnet 是 Python 模块,因此可执行文件保持常规 python.exe 从您安装的 Python 或 ZCCBD6784F30A2DCEB678. Supported both 32bit and 64bit.支持 32 位和 64 位。

Unlimited access to all C# classes, methods with output and ref parameters, generic classes and generic methods, extension methods, private members.无限制地访问所有 C# 类、具有 output 和 ref 参数的方法、泛型类和泛型方法、扩展方法、私有成员。

Overloaded assembly loader with customized mechanics for searching assemblies.具有用于搜索程序集的自定义机制的重载程序集加载器。

.NET runtime type information convertible to class object, which can be instantiated as any other class. .NET 运行时类型信息可转换为 class object,可以实例化为任何其他 ZA2F2ED4F8EBC2CBB16C21A29DCZ40。

Special import mode designed especially for Python interactive shell, which allows you to discover available assemblies, namespaces, classes, methods, etc.专为 Python 交互式 shell 设计的特殊导入模式,它允许您发现可用的程序集、命名空间、类、方法等。

I'm waiting for feedback:)我正在等待反馈:)

I am not a .NET expert, but you code looks as if your method is exposed as a COM object. 我不是.NET专家,但您的代码看起来好像您的方法是作为COM对象公开的。 So you can try http://starship.python.net/crew/mhammond/win32/ package to access it. 所以你可以试试http://starship.python.net/crew/mhammond/win32/包来访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM