- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在 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 属性的 super 方法,但我只是不知道如何做。
关于c# - 我如何从 IronPython 中的公共(public)固定字节读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10117287/
虽然我熟悉使用 PythonModule 程序集属性制作 .net 程序集,但我对如何制作子模块有点好奇。这会是类(class)中的类(class)吗?即:如果我有一个定义为 IronPython 模
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
有一个名为“Entities”的 IronPython 包。该包包含一个“Entity.py”文件,该文件定义了“Customer”类和“Address”类。 如果我运行这个程序: customer
所以我的数据表结构如下 ColumnName1 | ColumnName2 | ColumnName3 ------------|-------------|------------ value1
我想将 ironpython 作为脚本运行,而不是将 ironpython 代码编译成可执行文件。有没有办法将ironpython作为脚本运行? 最佳答案 是的,解释器安装在 Program File
我在 IronPython 中托管 IronPython。我没有找到使用等效的命令行参数初始化它的方法:-X:FullFrames . 我的代码有点像这样: import clr clr.AddRef
我们将 ironpython 嵌入到我们的应用程序中,以便脚本可以在我们的应用程序上下文中执行。 我使用 Python.CreateEngine() 和 ScriptScope.Execute() 来
我一直在考虑添加 IronPython 来扩展我支持的科学应用程序的想法。 这是一个好主意还是一个可怕的主意? 有没有在生产应用程序中使用 IronPython 的好例子。我看过 Resolver,它
我正在使用 IronPython 脚本来重置所有过滤器并设置一些文档属性。 “FUTUREONLY”下面的文档属性是一个下拉属性控件,根据表达式有 3 种可能的选择。当我运行脚本时,它会将文档属性重置
在查看了这两个项目中的每一个之后,似乎两者都非常相似。两者都运行在 CLI 之上,都有 Python 风格的语法,都使用 .NET 而不是标准的 Python 库。 那么,它们之间的区别和各自的优势是
我正在尝试使用 Microsoft.Web.Administration 库远程管理 IIS7 安装。 我在 IronPython 中执行此操作: import Microsoft.Web.Admin
有没有等同于Cython的东西对于 IronPython?也就是说,一种使用类型注释来编译简单函数以提高性能的方法?我知道 IronPython 的一个好处是能够快速地用 C# 或 F# 之类的东西编
我正在考虑使用 IronPython 开发一个小型应用程序,但是我想将我的应用程序分发给非技术人员,因此理想情况下我希望能够为他们提供我的应用程序的标准快捷方式以及他们需要的说明首先安装 IronPy
我在条形图上使用以下代码来切换维度。 myChart.XAxis.Expression = "[Sales]" myChart.BarWidth = 1
如何使用 Ironpython 返回 Spotfire 文件中文档属性的完整列表。 属性可以通过设置 Document.Properties["myProperty"] = "myValue" 我想做
我正在尝试使用 IronPython.Hosting 和 Pygments 为我的博客创建 ASP.NET 语法荧光笔。问题是,如果没有黑客攻击,Pygments 至少不能在 IPy 2.0 上运行。
我在 Windows XP (SP3) 系统上安装了 Aptana Studio 3 和 Eclipse 4.2.1(带 PyDev 和 PyDev 扩展)。我一直未能成功加载外部 .NET 程序集。
大家早上好, 我已经在这个问题上工作了几天,但我找不到解决办法。我已经研究并谷歌搜索无济于事。任何帮助/见解将不胜感激。我正在尝试创建一个按钮,单击该按钮时将自动通过日期过滤器(比如说从 1/1/15
想必大家都已经听说过some key developers leaving the Dynamic Languages team的消息了。由于他们认为 Microsoft 对动态语言的支持正在减弱。
我试图从经理的角度回答这个问题:如果我们用 IronPython 而不是 C# 编写应用程序,我们会招致什么整体性能损失? 这个问题是针对那些可能已经进行了一些测试、基准测试或已经完成从 C# 到 I
我是一名优秀的程序员,十分优秀!