5

我正在尝试将一个简单的 C# 命令行实用程序移植到 Python。C# 程序使用一个名为 foobar.dll 的自定义 .Net dll,它通过 I2C 与一些实验室设备连接。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)

我想过简单地使用 ctypes 来访问 dll,但我认为这不适用于 C#/.Net。由于我们实验室的限制,我需要使用普通的 Python 发行版 + 插件模块(即不是 IronPython 或 CPython。)我查看了 Python for .NET,但我不确定这是否真的有效。我是 .Net 的 n00b,但在 Java、C++ 和 Python 方面拥有丰富的经验。以前有人遇到过这种情况和好的解决方案吗?

编辑:

有了下面的解释,我在 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].

然后解释器崩溃了。这看起来像是一个糟糕的 dll,对吗?

4

1 回答 1

7

Python for .NET运行良好,您可以在 Python 中执行 .NET 代码。IronPython 也可以很好地工作,尽管反过来。您可以将 Python for .NET 视为可以执行 .NET 代码的 CPython 的扩展,而 IronPython 是允许执行 Python 代码的 CLR 的扩展。由于您的需求要求您使用普通 Python,因此适用于 .NET 的 Python 应该可以工作。需要注意的,在大多数情况下,vanilla python 发行版是CPython。

于 2011-04-25T21:44:39.290 回答