如何在 python 中加载 C# dll?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2077870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load a C# dll in python?
提问by aF.
how can I load a c# dll in python?
如何在 python 中加载 ac# dll?
Do I have to put some extra code in the c# files? (like export in c++ files)
我是否必须在 c# 文件中添加一些额外的代码?(如在 C++ 文件中导出)
I don't want to use IronPython. I want to import a module to Python!
我不想使用 IronPython。我想导入一个模块到 Python!
采纳答案by Vivek Bernard
This is to answer the second part of your Question Try making the DLL COM visible.
这是回答问题的第二部分尝试使 DLL COM 可见。
by using the
通过使用
[ComVisible(true)]
Ok IronPython is a .net implemenatation of the Python language The technology is going to use the DLR of the .net 4.0 when it arrives so IronPython will have more Dynamism (is that a word). (In english if you're a Python guru, you'll feel more at home when you use IronPython)
好的 IronPython 是 Python 语言的 .net 实现 该技术将在它到来时使用 .net 4.0 的 DLR,因此 IronPython 将具有更多的动态性(是一个词)。(用英语来说,如果您是 Python 大师,使用 IronPython 时会感觉更自在)
So you may well choose IronPython, if you do that you can skip the COM visible part. Since both (C# , Iron Python) are under .Net
所以你很可能会选择 IronPython,如果你这样做,你可以跳过 COM 可见部分。由于两者(C#,Iron Python)都在 .Net 下
回答by Warren
Python for .NETworks well if you don't want to use IronPython.
如果您不想使用 IronPython,用于 .NET 的 Python运行良好。
回答by Vincent
The package Python for.NETand the Python Implementation IronPythonnow work the same way.
Python for .NET包和 Python 实现IronPython现在以相同的方式工作。
Example for a C# DLL MyDll.dll
:
C# DLL 的示例MyDll.dll
:
import clr
clr.AddReference('MyDll')
from MyNamespace import MyClass
my_instance = MyClass()
See this postfor more details.
有关更多详细信息,请参阅此帖子。
回答by anhoppe
If you do not want to use solutions like Python .NET or IronPython it is possible to implement a C++/CLI wrapper and use Pythons ctypes in order to load it. For example:
如果您不想使用 Python .NET 或 IronPython 之类的解决方案,则可以实现 C++/CLI 包装器并使用 Pythons ctypes 来加载它。例如:
The C++/CLI library CallCSharp:
C++/CLI 库 CallCSharp:
extern "C" {
__declspec(dllexport) void foo()
{
// here you could use managed and unmanaged code
Console.WriteLine("Hello, C# world...");
}
The Python script:
Python脚本:
from ctypes import cdll
lib = cdll.LoadLibrary("./CallCSharp.dll")
lib.foo()
I strongly recomment reading this blog: http://pragmateek.com/if-your-plumbing-doesnt-work-youre-just-not-using-enough-pipes/#more-1745
我强烈建议阅读此博客:http: //pragmateek.com/if-your-plumbing-doesnt-work-youre-just-not-using-enough-pipes/#more-1745
It also handles the issue that arises when the C++/CLI wrapper calls code that is in another assembly (you'd get something like a WindowsError: [Error -532462766] Windows Error 0xE0434352 from your Python script then).
它还处理当 C++/CLI 包装器调用另一个程序集中的代码时出现的问题(然后你会从你的 Python 脚本中得到类似 WindowsError: [Error -532462766] Windows Error 0xE0434352 的信息)。