如何将Lua与.Net集成
问题描述:
要求是用户应该能够在文本框中添加 Lua 脚本,然后我需要检查用户是否添加了正确的 Lua 脚本,如果脚本正确,那么我需要运行该脚本.谁能建议我一些代码?我尝试了以下操作:
Requirement is user should be able to add Lua script in text box, and then I need to check user has added proper Lua script and if script is correct then I need to run that script. Can anyone suggest me some code? I tried following:
using (Lua state = new Lua())
{
state.DoString(txt.Text);
var scriptFunc = state["ScriptFunc"] as LuaFunction;
var res = scriptFunc.Call(2,3);
Response.Write(res);
}
答
您正在寻找的是 Moon#
( http://www.moonsharp.org/).因此,您的示例将如下所示:
What you are looking for is Moon#
(http://www.moonsharp.org/). So your example will look like this:
double MoonSharpFactorial2()
{
string scriptCode = @"
-- defines a factorial function
function fact (n)
if (n == 0) then
return 1
else
return n*fact(n - 1)
end
end";
Script script = new Script();
script.DoString(scriptCode);
DynValue res = script.Call(script.Globals["fact"], 4);
return res.Number;
}