使用 Lua 中基于标志的 .NET 枚举

发布于 2025-01-05 23:29:42 字数 1474 浏览 0 评论 0原文

我正在使用 LuaInterface for .NET 创建 Windows 窗体对象。除了一件事之外,它的效果非常好:

我想使用 ControlAnchor 属性来使它们自动调整大小。如果我只设置一个锚点(例如仅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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花坊 2025-01-12 23:29:42

我终于想出了如何做到这一点。我使用了 System.Enum 的 ToObject 方法。它需要我想要将其转换为的枚举类型以及要使用的整数值。

以下是我的帮助程序库中的代码片段:

local EnumToObject, WinFormsAnchorStylesType = 
                luanet.get_method_bysig(luanet.System.Enum, "ToObject",
                                             "System.Type", "System.Int32"),
                luanet.System.Windows.Forms.AnchorStyles.Top:GetType()

AnchorTop, AnchorLeft, AnchorRight, AnchorBottom = 1, 4, 8, 2

function Anchor(flags)
  return EnumToObject(WinFormsAnchorStylesType, flags)
end

您可以像这样使用它:

Label1 = luanet.System.Windows.Forms.Label()
Label1.Anchor = Anchor(AnchorLeft + AnchorTop + AnchorRight + AnchorBottom)

I finally figured out how to do this. I used the ToObject method of System.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:

local EnumToObject, WinFormsAnchorStylesType = 
                luanet.get_method_bysig(luanet.System.Enum, "ToObject",
                                             "System.Type", "System.Int32"),
                luanet.System.Windows.Forms.AnchorStyles.Top:GetType()

AnchorTop, AnchorLeft, AnchorRight, AnchorBottom = 1, 4, 8, 2

function Anchor(flags)
  return EnumToObject(WinFormsAnchorStylesType, flags)
end

You use it like this:

Label1 = luanet.System.Windows.Forms.Label()
Label1.Anchor = Anchor(AnchorLeft + AnchorTop + AnchorRight + AnchorBottom)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文