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

标签 c# scripting lua luainterface

我正在尝试使用 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 实例,然后通过本地引用完成所有工作(block 其余部分的所有代码都可以使用):

--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 来避免上面的三步名称保留舞蹈 (local luainterface=luanet; luanet=nil; local luanet=luainterface)然后通过对全局表的 _G 引用删除全局:

local luanet=_G.luanet
_G.luanet = nil

我只是出于个人喜好选择不这样做。)

关于c# - LuaInterface - 如何限制对 .Net 类的访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6171381/

相关文章:

c# - 从excel中排序列表

c# - 与 WindowChrome 一起使用时,窗口模板中的边框边距没有任何效果

linux - 在Linux上检索eth0 ipv6地址

linux - 如何从 Bash 脚本列出目标 PID 的曾孙 PID?

c++ - 用于多线程环境的带有 C/C++ API 的嵌入式脚本语言

c# - 在构造函数 c# 中设置程序集标题

c# - 在 C# 中使用 Canon SDK 时出现 SDK 错误 : 0x8D07,

linux - sed 提取给定日期之间的日志

lua - 很棒的 wm 中的 cpu 温度小部件

string - 从 5.3 开始,Lua format.string 无法将 float 格式化为十进制 (%d)