--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 的全局和存储实例,然后通过本地引用完成所有工作(块其余部分中的所有代码都可以使用):
--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...
local luainterface=luanet; luanet=nil; local luanet=luainterface
(您可以通过直接本地化到luanet
然后通过_G
对全局表的引用删除全局来避免上面()的三步名称保留舞蹈:
local luanet=_G.luanet
_G.luanet = nil
我只是出于个人喜好选择不这样做。)