加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – LuaInterface – 如何限制对.Net类的访问?

发布时间:2020-12-15 08:45:06 所属栏目:百科 来源:网络整理
导读:我正在尝试使用LuaInterface 2.0.3在我的C#应用??程序中嵌入一些Lua脚本功能.到目前为止,这工作正常,但我无法弄清楚如何限制只访问少数指定的.Net类.默认情况下,所有.Net库都可以通过“luanet”直接访问,Lua脚本可以自由打开新窗口或访问文件系统. 例如这个L
我正在尝试使用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

我只是选择不作为个人喜好.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读