简体   繁体   English

在Python脚本中使用.Net(C#)dll

[英]Use .Net (C#) dll in Python script

I'm attempting to port a simple C# command-line utility to Python. 我正在尝试将一个简单的C#命令行实用程序移植到Python。 The C# program uses a custom .Net dll called foobar.dll that interfaces with some lab equipment via I2C. C#程序使用一个名为foobar.dll的自定义.Net dll,它通过I2C与一些实验室设备连接。 The usage of the dll in the C# code is as follows: C#代码中dll的用法如下:

fooBar.fooProvider provider = new foobar.fooProvider()
fooBar.fooBarLib fooLib = new foobar.fooBarLib(provider, 0x80)
# used below ...
numFloat = fooLib.GetSomething()
# more python ...
fooLib.SetSomething(NumberAsAFloat)

I thought about simply using ctypes to access the dll, but I figured this wouldn't work for C#/.Net. 我想过简单地使用ctypes访问dll,但我认为这对C#/ .Net不起作用。 Due to restrictions in our lab I need to use a vanilla Python distro + addon modules (ie not IronPython or CPython.) I looked at Python for .NET, but I'm not sure this would actually work. 由于我们实验室的限制,我需要使用一个vanilla Python发行版+插件模块(即不是IronPython或CPython。)我看了Python for .NET,但我不确定这实际上是否有用。 I'm a n00b to .Net, but have lots of experience with Java, C++ & Python. 我是.Net的n00b,但在Java,C ++和Python方面有很多经验。 Anyone encountered this situation and a good solution before? 以前有人遇到过这种情况和一个好的解决方案

EDIT: 编辑:

With the explanation from below in hand, I imported the clr in the python interpreter, then attempted to import the fooBar library with the following result: 通过下面的解释,我在python解释器中导入了clr,然后尝试导入fooBar库,结果如下:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Interop.AVMCIFCLib, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
File name: 'Interop.AVMCIFCLib, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null'
   at System.Signature._GetSignature(SignatureStruct& signature, Void* pCorSig, Int32 cCorSig, IntPtr fieldHandle, IntPtr methodHandle, IntPtr declaringTypeHandle)
   at System.Signature.GetSignature(SignatureStruct& signature, Void* pCorSig, Int32 cCorSig, RuntimeFieldHandle fieldHandle, RuntimeMethodHandle methodHandle, RuntimeTypeHandle declaringTypeHandle)
   at System.Signature..ctor(RuntimeMethodHandle methodHandle, RuntimeTypeHandledeclaringTypeHandle)
   at System.Reflection.RuntimeMethodInfo.get_Signature()
   at System.Reflection.RuntimeMethodInfo.FetchNonReturnParameters()
   at System.Reflection.RuntimeMethodInfo.GetParametersNoCopy()
   at System.Reflection.RuntimePropertyInfo.GetIndexParameters()
   at Python.Runtime.ClassManager.GetClassInfo(Type type)
   at Python.Runtime.ClassManager.CreateClass(Type type)
   at Python.Runtime.ClassManager.GetClass(Type type)
   at Python.Runtime.ModuleObject.GetAttribute(String name, Boolean guess)
   at Python.Runtime.ModuleObject.LoadNames()
   at Python.Runtime.ImportHook.__import__(IntPtr self, IntPtr args, IntPtr kw)

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

And then the interpreter crashes. 然后解释器崩溃了。 This looks like a badly built dll, correct? 这看起来像一个糟糕的DLL,对吗?

Python for .NET works really well, and you can execute .NET code from within Python. Python for .NET运行良好,您可以在Python中执行.NET代码。 IronPython works well too, although the other way around. IronPython也运作良好,但另一种方式。 You can think of Python for .NET as an extension of CPython that can execute .NET code, while IronPython is an extension of the CLR allowing to execute Python code. 您可以将Python for .NET视为可以执行.NET代码的CPython的扩展,而IronPython是允许执行Python代码的CLR的扩展。 Since your needs dictate you to use a vanilla Python, Python for .NET should work. 由于您的需求要求您使用vanilla Python,因此Python for .NET应该可行。 And to note, the vanilla python distribution is CPython in most cases. 值得注意的是,在大多数情况下,vanilla python发行版都是 CPython。

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

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