使用 Python.NET 将 .NET 类型 (System.Converter) 转换为 Python 类型

Converting a .NET type (System.Converter) to a Python type with Python.NET

我正在尝试编写 Python.NET 相当于 this C# code:

Util.ArrayInit(motifPositionData[i], j => backgroundDist.Sample());

但是,the Util.ArrayInit() method accepts an int and a Converter<TInput,TOutput> Delegate --- 我不知道 Python 的等价物是什么。我尝试了 lambda,但没有用:

Util.ArrayInit(1, lambda x: 1)
TypeError: No method matches given arguments for ArrayInit: (<class 'int'>, <class 'function'>)

问题 1:如何从 Python debugger/console 中检查哪些参数确实具有匹配方法? (也许不可能。)

问题二: 我是否需要从 Python function(例如 lambda)到 .NET System.Converter 创建一个 custom object marshalling,就像 Python int 会自动转换为 .NET System.Int32... 或者有更简单的方法吗?


我一开始就没有使用 Util.ArrayInit 来规避这个问题,但我仍然对这样做的情况感到好奇。

>>> import clr
>>> from System import Converter
>>> c = Converter[int, str](lambda i: str(i))
>>> c(42)
'42'