简体   繁体   中英

How does one retrieve a c# byte array (Byte[]) from IronPython?

I have ac# function that I can call from IronPython. The function returns a byte array that I'd like to convert to a string for display and compare.

Python is telling me to pass the input parameter - (out Byte[] DataOut), below - as type "StrongBox[Array[Byte]]", so I converted "var" with

clr.Reference[Array[Byte]]() .

How do I convert this to a string?

namespace My_Library.My_Namespace
{
    /// </summary>
    public class My_App : OSI_Layer
    {

        public bool My_Function(out Byte[] DataOut)
        {

        // fill up DataOut with a string 

            return (Send(out DataOut));
        }

    // etc...
    }

}

//////////////////////////
//
// IronPython
//
// From IronPython I...

>>>
>>> import clr
>>> clr.AddReferenceToFileAndPath('My_Library.dll')
>>> from My_Library.My_Namespace import My_App
>>> App = My_App()
>>>
>>> from System import Array, Byte      
>>> var = clr.Reference[Array[Byte]]() # Create type StrongBox[Array[Byte]]"
>>>
>>> clr.Reference[Array[Byte]]
<type 'StrongBox[Array[Byte]]'>
>>>
>>> App.My_Function(var)
>>>
True
>>> var
<System.Byte[] object at 0x000000000000002B [System.Byte[]]>
>>>
>>> printable_var = System.BitConverter.ToString(var)

Traceback (most recent call last): File "", line 1, in TypeError: expected Array[Byte], got StrongBox[Array[Byte]]

您需要传入框的Value ,而不是框本身。

printable_var = System.BitConverter.ToString(var.Value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM