简体   繁体   English

如何从IronPython中的公共固定字节读取?

[英]How do I read from a public fixed byte in IronPython?

In C# I have an attribute declared as: 在C#中,我有一个声明为的属性:

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: 在IronPython打印中,它给了我一个字符串类型:

>>> 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? 在IronPython中读取此属性的正确方法是什么?

Fixed fields in .NET are quite special. .NET中的固定字段非常特殊。 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. 您拥有的固定字段( public fixed byte foo[10] )将被编译为特殊的嵌套结构,并且固定字段的类型将更改为该嵌套结构。 In short, this: 简而言之,这个:

public fixed byte foo[10];

Gets 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;

You can see this for yourself with a tool like ILSpy. 您可以使用ILSpy之类的工具自己查看。

Now, if your code in C# has a line GetString(obj.foo) it is compiled into: 现在,如果你在C#中的代码有一行GetString(obj.foo)它将编译成:

GetString(&obj.foo.FixedElementField);

So it literally takes the address of the first element of your array and passes it as the parameter to the method (thus the GetString parameter is of the correct type, byte* ). 因此,它从字面上获取数组的第一个元素的地址,并将其作为参数传递给方法(因此GetString参数的类型正确,为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 to byte* (obviously). 当你在IronPython中使用相同的参数调用相同的方法时,参数类型仍然是你的字段的类型: <foo>e__FixedBuffer0 ,它不能转换为byte* (显然)。 The correct method of making this method call would be to do the same substitution as the C# compiler does - take the address of the FixedElementField and pass it to the GetString , but unfortunately, Python (to my knowledge) does not have an analog to the & operator in C#. 调用此方法的正确方法是执行与C#编译器相同的替换 - 获取FixedElementField的地址并将其传递给GetString ,但不幸的是,Python(据我所知)没有模拟& C#中的运算符。

The conclusion would be: you cannot directly access a fixed field from IronPython. 结论是:您不能直接从IronPython访问固定字段。 I would say that your best bet is to have a "proxy" method like: 我想说你最好的选择就是采用“代理”方法:

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

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. PS我不是IronPython专业人士,所以也许有一种超级方式可以直接访问foo prop,但我只是不知道如何。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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