繁体   English   中英

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

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

在C#中,我有一个声明为的属性:

public fixed byte foo[10]

在客户端代码中,我看到它使用此函数转换为字符串:

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

在IronPython打印中,它给了我一个字符串类型:

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

尝试使用转换函数会出错。

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

在IronPython中读取此属性的正确方法是什么?

.NET中的固定字段非常特殊。 您拥有的固定字段( public fixed byte foo[10] )将被编译为特殊的嵌套结构,并且固定字段的类型将更改为该嵌套结构。 简而言之,这个:

public fixed byte foo[10];

获取编译成:

// 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之类的工具自己查看。

现在,如果你在C#中的代码有一行GetString(obj.foo)它将编译成:

GetString(&obj.foo.FixedElementField);

因此,它从字面上获取数组的第一个元素的地址,并将其作为参数传递给方法(因此GetString参数的类型正确,为byte* )。

当你在IronPython中使用相同的参数调用相同的方法时,参数类型仍然是你的字段的类型: <foo>e__FixedBuffer0 ,它不能转换为byte* (显然)。 调用此方法的正确方法是执行与C#编译器相同的替换 - 获取FixedElementField的地址并将其传递给GetString ,但不幸的是,Python(据我所知)没有模拟& C#中的运算符。

结论是:您不能直接从IronPython访问固定字段。 我想说你最好的选择就是采用“代理”方法:

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

PS我不是IronPython专业人士,所以也许有一种超级方式可以直接访问foo prop,但我只是不知道如何。

暂无
暂无

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

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