In C# I have an attribute declared as:
public fixed byte foo[10]
In client code I’m see it uses this function to convert to string:
public static unsafe string GetString(byte* byteArray)
{
return new String((sbyte*)byteArray);
}
In IronPython printing it given me the 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 read this attribute in IronPython?
Fixed fields in .NET are quite special. A fixed field that you have (
public fixed byte foo[10]) gets compiled into a special nested struct and the type of your fixed field is changed into that nested struct. In short, this:Gets compiled into this:
You can see this for yourself with a tool like ILSpy.
Now, if your code in C# has a line
GetString(obj.foo)it is compiled into:So it literally takes the address of the first element of your array and passes it as the parameter to the method (thus the
GetStringparameter is of the correct type,byte*).When you call the same method with the same parameter in IronPython the parameter type is still the type of your field:
<foo>e__FixedBuffer0, which cannot be cast tobyte*(obviously). The correct method of making this method call would be to do the same substitution as the C# compiler does – take the address of theFixedElementFieldand pass it to theGetString, but unfortunately, Python (to my knowledge) does not have an analog to the&operator in C#.The conclusion would be: you cannot directly access a fixed field from IronPython. I would say that your best bet is to have a “proxy” method like:
PS I am not an IronPython pro, so maybe there is a super-way to directly access the foo prop, but I just do not see how.