我正在尝试使用LuaInterface 2.0.3在我的C#应用程序中嵌入一些Lua脚本功能.到目前为止,这工作正常,但我无法弄清楚如何限制只访问少数指定的.Net类.默认情况下,所有.Net库都可以通过“luanet”直接访问,Lua脚本可以自由打开新窗口或访问文件系统.
例如这个Lua脚本将打开一个新窗口:
Form = luanet.System.Windows.Forms.Form mainForm = Form() mainForm:ShowDialog()
编写脚本的自由很棒,但这可能会干扰托管应用程序,并且具有一些我不太喜欢的与安全相关的含义.有没有办法禁用它?
解决方法
--make a table for all the classes you want to expose safeClasses = {} --store all the ones you want safeClasses.Form = luanet.System.Windows.Forms.Form --etc... --remove access to LuaInterface luanet = nil package.loaded.luanet = nil --prevent future packages from being loaded require = nil package.loadlib = nil
您也可以反向执行此操作,首先删除LuaInterface的全局和存储实例,然后通过本地引用(其余0700中的所有代码都可以使用)完成所有工作:
--get a local reference to LuaInterface without clobbering the name local luainterface = luanet --delete the global reference to it luanet = nil --also delete it from the package store and disable package loading package.loaded.luanet = nil require = nil package.loadlib = nil --put luanet back locally at its original name (for convenience) local luanet = luainterface --make a table for all the classes you want to expose safeClasses = {} --store all the ones you want safeClasses.Form = luanet.System.Windows.Forms.Form --etc...
(你可以通过直接本地化到luanet然后通过_G引用删除全局表来避免上面的三步名称保存舞(本地luainterface = luanet; luanet = nil; local luanet = luainterface):
local luanet=_G.luanet _G.luanet = nil
我只是选择不作为个人喜好.)