Python 到 C# StongBox[单]

Python to C# StongBox[Single]

我使用 IronPython 添加了对 C# dll 的引用。我试图在 DLL 中使用一个方法,它需要一个类型的参数:

out float tempValue

当我将 python 浮点对象传递给方法时,我得到以下回溯:

Traceback (most recent call last):
  File "MeasurementComputing.py", line 20, in <module>
TypeError: expected StrongBox[Single], got float

我的问题是:

  1. 什么是保险箱[Single]
  2. 如何在 python 中创建这样一个对象以传递给 C# 方法。

为了为 out 参数设置一个正确的目标,您必须在 IronPython 中显式创建一个 clr 引用(StrongBox 作为 reference/value 包装器),因为没有 out 关键字允许您这样做的调用方(如在 C# 中)。

这可能看起来像:

import clr
import System
tempValue = clr.Reference[System.Single]()

直接创建 StrongBox 实例应该也可以。

请注意,您也可以使用只有输出参数的方法,除了接收元组之外不传递任何内容,如 here 所述。