在 Python 中使用 Python .Net 初始化 Array[int]

Initializing an Array[int] with Python .Net in Python

我正在使用 Python .Net library so I don't have access to List<T>。目前,当我尝试初始化一个数组时,会抛出一个错误。

documentation 有一个示例从最新的代码库中抛出错误。

例如:

from System import Array
myarray = Array[int](10) #TypeError: Cannot convert 10 to System.Int32[]

以下内容适用于 64 位但不适用于 32 位!

myarray = Array[int]([10]) #OverflowError: value too large to convert

我在执行Array[int](10)时也出现错误。看起来预期的参数不是大小,而是 Python 列表。以下对我有用:

py_array = [1, 2, 3]
net_array = Array[int](py_array)

现在,net_array.Length 应该 return 3.

你可以这样写:

myarray = Array[int]

使用System.Array.CreateInstance(<type>, <length>)

参考msdn