Lua Interface

lua-users home
wiki

LuaInterface 是一个用于集成 Lua 语言和 Microsoft .NET 公共语言运行库 (CLR) 的库。Lua 脚本可以使用它来实例化 CLR 对象、访问属性、调用方法,甚至使用 Lua 函数来处理事件。

网站

依赖项: .NET。在 Linux 上的 Mono 运行时 http://www.mono-project.com 上也能工作。

注意:版本 1.5.3 构建了一个可以加载到常规 Lua 解释器中的 C 模块 DLL。版本 2.0 及以上完全是托管代码。 [4][5][6][7][2][3]

用户评论

LuaInterface 文档中没有提到如何实例化 .NET 数组。这可以通过简单地通过数字索引类型引用来实现。

local a = SomeType[5]
for i = 1,a.Length do
	print(i, a[i - 1]) -- .NET arrays are zero-based.
end


[ LuaInterface-1.3.0 添加了以下功能 ]

当从 Lua 使用 CLR 时,加载类型和类的常规方法可能显得有点冗长。

net = require "luainterface"

net.load_assembly("System.Windows.Forms")
net.load_assembly("System.Drawing")

Form = net.import_type("System.Windows.Forms.Form")
Button = net.import_type("System.Windows.Forms.Button")
Point = net.import_type("System.Drawing.Point")

local form1 = Form()
local button1 = Button()
button1.Location = Point(10, 10)

如果可以使用这样的语法就好了

require "net"

Form = net.System.Windows.Forms.Form
Button = net.System.Windows.Forms.Button
Point = net.System.Drawing.Point

local form1 = Form()
local button1 = Button()
button1.Location = Point(10, 10)

可选地,可以将 .NET 类型显示为顶层名称。

require "net"

System = net.System

Form = System.Windows.Forms.Form
Button = System.Windows.Forms.Button
Point = System.Drawing.Point

local form1 = Form()
local button1 = Button()
button1.Location = Point(10, 10)

不应该有必要使每个类型名称都成为顶层名称。

require "net"

System = net.System

local form1 = System.Windows.Forms.Form()
local button1 = System.Windows.Forms.Button()
button1.Location = System.Drawing.Point(10, 10)

这是方法:

-- Create a metatable for our name components
local metatable = { [".NET"] = {getmetatable=getmetatable} }

-- Load LuaInterface
local g = getfenv(0)
setfenv(0, metatable[".NET"])
local init, e1, e2 = loadlib("LuaInterfaceLoader.dll", "luaopen_luainterface")
assert(init, (e1 or '') .. (e2 or ''))
init()
setfenv(0, g)

-- Lookup a .NET identifier component.
function metatable:__index(key) -- key is e.g. "Form"
    local mt = getmetatable(self)
    local luanet = mt[".NET"]

    -- Get the fully-qualified name, e.g. "System.Windows.Forms.Form"
    local fqn = ((self[".fqn"] and self[".fqn"] .. ".") or "") .. key

    -- Try to find either a luanet function or a CLR type
    local obj = luanet[key] or luanet.import_type(fqn)

    -- If key is neither a luanet function or a CLR type, then it is simply
    -- an identifier component.
    if obj == nil then
		-- It might be an assembly, so we load it too.
        luanet.load_assembly(fqn)
        obj = { [".fqn"] = fqn }
        setmetatable(obj, mt)
    end

    -- Cache this lookup
    self[key] = obj
    return obj
end

-- A non-type has been called; e.g. foo = System.Foo()
function metatable:__call(...)
    error("No such type: " .. self[".fqn"], 2)
end

-- This is the root of the .NET namespace
net = { [".fqn"] = false }
setmetatable(net, metatable)

-- Preload the mscorlib assembly
net.load_assembly("mscorlib")

return nil

另请参阅


RecentChanges · preferences
编辑 · 历史
最后编辑于 2011 年 7 月 26 日凌晨 1:25 GMT (差异)