How can I read the public fixed byte in IronPython?

In C #, I have an attribute declared as:

public fixed byte foo[10]

In the client code, I see that this function is used to convert to a string:

public static unsafe string GetString(byte* byteArray)
{
  return new String((sbyte*)byteArray);
}

In IronPython print, he gave me a type as a string:

>>> print obj.foo
Baz+<foo>e__FixedBuffer1

Trying to use the conversion function gives an error.

>>> print GetString(obj.foo)
expected Byte*, got <Foo>e__FixedBuffer1

What is the correct way to read this attribute in IronPython?

+3
source share
1 answer

Fixed fields in .NET are very special. The fixed field that you have ( public fixed byte foo[10]) is compiled into a special nested structure, and the type of the fixed field changes to this nested structure. In short, this is:

public fixed byte foo[10];

It turns out compiled into this:

// This is the struct that was generated, it contains a field with the
// first element of your fixed array
[CompilerGenerated, UnsafeValueType]
[StructLayout(LayoutKind.Sequential, Size = 10)]
public struct <foo>e__FixedBuffer0
{
    public byte FixedElementField;
}

// This is your original field with the new type
[FixedBuffer(typeof(byte), 10)]
public <foo>e__FixedBuffer0 foo;

, ILSpy.

, # GetString(obj.foo), :

GetString(&obj.foo.FixedElementField);

, ( , GetString , byte*).

IronPython, - : <foo>e__FixedBuffer0, byte* (). , , #, - FixedElementField GetString, , , Python ( ) & #.

: IronPython. , "":

public string GetFooString(Baz baz)
{
    return new string((sbyte*)baz.foo);
}

PS IronPython, , , - foo prop, , .

+6

All Articles