开发者

Convert between python array and .NET Array

开发者 https://www.devze.com 2023-01-02 22:09 出处:网络
I have a python method that returns a Python byte array.array(\'c\'). Now, I want to copy t开发者_运维知识库his array using System.Runtime.InteropServices.Marshal.Copy. This method however expects a

I have a python method that returns a Python byte array.array('c').

Now, I want to copy t开发者_运维知识库his array using System.Runtime.InteropServices.Marshal.Copy. This method however expects a .NET array.

import array
from System.Runtime.InteropServices import Marshal

bytes = array.array('c')
bytes.append('a')
bytes.append('b')
bytes.append('c')
Marshal.Copy(bytes, dest, 0, 3)

Is there a way to make this work without copying the data? If not, how do I convert the data in the Python array to the .NET array?


To convert a python array to a .NET Array:

import array
from System import Array, Char

x = array.array('c', 'abc')

y = Array[Char](x)

Here is some information on creating typed Arrays in IronPython: http://www.ironpython.info/index.php?title=Typed_Arrays_in_IronPython

0

精彩评论

暂无评论...
验证码 换一张
取 消