Assigning a Iron Python list to .NET array

前提是你 提交于 2019-12-21 17:50:31

问题


I have a list comprehension operating on elements of an .NET array like

obj.arr = [f(x) for x in obj.arr]

However the assignment back to obj.arr fails.

Is it possible to convert a list to a .NET array in IronPython?


回答1:


Try this:

obj.arr = Array[T]([f(x) for x in obj.arr])

replacing T with type of array elements.

Alternatively:

obj.arr = tuple([f(x) for x in obj.arr])



回答2:


Arrays have to be typed as far as I know. This works for me:

num_list = [n for n in range(10)]

from System import Array
num_arr = Array[int](num_list)

Similarly for strings and other types.



来源:https://stackoverflow.com/questions/1107789/assigning-a-iron-python-list-to-net-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!