使用 Lua 中基于标志的 .NET 枚举
我正在使用 LuaInterface for .NET 创建 Windows 窗体对象。除了一件事之外,它的效果非常好:
我想使用 Control
的 Anchor
属性来使它们自动调整大小。如果我只设置一个锚点(例如仅AnchorStyles.Top
),它可以工作,但这实际上没有意义。我必须设置多个锚点,这是通过将它们与“按位或”组合(或者仅通过数字相加)来完成的。
在 VB.Net 中,这两种方法都有效:
Dim myLabel As New Label()
myLabel.Anchor = AnchorStyles.Top
myLabel.Anchor = AnchorStyles.Top + AnchorStyles.Left + _
AnchorStyles.Bottom + AnchorStyles.Right
在 Lua 中,这确实有效:
luanet.load_assembly("System.Windows.Forms")
local WinForms = luanet.System.Windows.Forms
local myLabel = WinForms.Label()
myLabel.Anchor = WinForms.AnchorStyles.Top
...但是这附加行不起作用:
myLabel.Anchor = WinForms.AnchorStyles.Top + WinForms.AnchorStyles.Left +
WinForms.AnchorStyles.Bottom + WinForms.AnchorStyles.Right
它给出了以下错误:
LuaInterface.LuaException: attempt to perform arithmetic on
field 'Top' (a userdata value)
从某种意义上来说,这是正确的,因为“LuaInterface 将枚举值视为相应枚举的字段typ”(说 LuaInterface:使用以下命令编写 .NET CLR 脚本卢阿)。
也不可能将该值分配为数字:
myLabel.Anchor = 15 -- 15 = 8 + 4 + 2 + 1 = Top+Left+Right+Bottom
这一次,错误消息相当不具体:
LuaInterface.LuaException: function
我该如何解决这个问题?
是否有可能将数字转换为 Lua 中正确的枚举类型?
I'm using LuaInterface for .NET to create Windows Forms objects. This works pretty good except for one thing:
I want to use the Anchor
property of Control
to make them resize automatically. If I only set one of the Anchors (e.g. only AnchorStyles.Top
), it works, but this doesn't really make sense. I have to set more than one Anchor, which is done by combining them with "bit-wise or" (or by just adding them numerically).
In VB.Net both works:
Dim myLabel As New Label()
myLabel.Anchor = AnchorStyles.Top
myLabel.Anchor = AnchorStyles.Top + AnchorStyles.Left + _
AnchorStyles.Bottom + AnchorStyles.Right
In Lua, this does work:
luanet.load_assembly("System.Windows.Forms")
local WinForms = luanet.System.Windows.Forms
local myLabel = WinForms.Label()
myLabel.Anchor = WinForms.AnchorStyles.Top
...but this additional line doesn't:
myLabel.Anchor = WinForms.AnchorStyles.Top + WinForms.AnchorStyles.Left +
WinForms.AnchorStyles.Bottom + WinForms.AnchorStyles.Right
It gives me the following error:
LuaInterface.LuaException: attempt to perform arithmetic on
field 'Top' (a userdata value)
which is in a sense correct as "LuaInterface treats enumeration values as fields of the corresponding enumeration typ" (says LuaInterface: Scripting the .NET CLR with Lua).
It is also not possible to assign the value as a number:
myLabel.Anchor = 15 -- 15 = 8 + 4 + 2 + 1 = Top+Left+Right+Bottom
This time, the error message is rather unspecific:
LuaInterface.LuaException: function
How can I work around this?
Is there a possibility to typecast the number to the correct enumeration type in Lua?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于想出了如何做到这一点。我使用了 System.Enum 的 ToObject 方法。它需要我想要将其转换为的枚举类型以及要使用的整数值。
以下是我的帮助程序库中的代码片段:
您可以像这样使用它:
I finally figured out how to do this. I used the
ToObject
method ofSystem.Enum
. It takes the enumeration type I want to convert it to, and the integer value to use.The following is a code snippet from my helper library:
You use it like this: