NLua Example
来源:互联网 发布:伊犁知乎 编辑:程序博客网 时间:2024/06/05 16:48
https://github.com/NLua/NLua
Example:You can use/instantiate any .NET class without any previous registration or annotation.
public class SomeClass { public string MyProperty {get; private set;} public SomeClass (string param1 = "defaulValue") { MyProperty = param1; } public int Func1 () { return 32; } public string AnotherFunc (int val1, string val2) { return "Some String"; } public static string StaticMethod (int param) { return "Return of Static Method"; }
Creating Lua state:
using NLua; Lua state = new Lua ()
Evaluating simple expressions:
var res = state.DoString ("return 10 + 3*(5 + 2)")[0] as double; // Lua can return multiple values, for this reason DoString return a array of objects
Passing raw values to the state:
double val = 12.0; state ["x"] = val; // Create a global value 'x' var res = state.DoString ("return 10 + x*(5 + 2)")[0] as double;
Retrieving global values:
state.DoString ("y = 10 + x*(5 + 2)"); var y = state ["y"] as double; // Retrieve the value of y
Retrieving Lua functions:
state.DoString (@" function ScriptFunc (val1, val2) if val1 > val2 then return val1 + 1 else return val2 - 1 end end "); var scriptFunc = state ["ScriptFunc"] as LuaFunction; var res = (int)scriptFunc.Call (3, 5).First (); // LuaFunction.Call will also return a array of objects, since a Lua function // can return multiple values
Using the .NET objects.
Passing .NET objects to the state:
SomeClass obj = new SomeClass ("Param"); state ["obj"] = obj; // Create a global value 'obj' of .NET type SomeClass // This could be any .NET object, from BCL or from your assemblies
Using .NET assemblies inside Lua:
To access any .NET assembly to create objects, events etc inside Lua you need to ask NLua to use CLR as a Lua package.To do this just use the methodLoadCLRPackage
and use the import
function inside your Lua script to load the Assembly.
state.LoadCLRPackage (); state.DoString (@" import ('MyAssembly', 'MyNamespace') import ('System.Web') "); // import will load any .NET assembly and they will be available inside the Lua context.
Creating .NET objects:To create object you only need to use the class name with the()
.
state.DoString (@" obj2 = SomeClass() -- you can suppress default values. client = WebClient() ");
Calling instance methods:To call instance methods you need to use the :
notation, you can call methods from objects passed to Lua or to objects created inside the Lua context.
state.DoString (@" local res1 = obj:Func1() local res2 = obj2:AnotherFunc (10, 'hello') local res3 = client:DownloadString('http://nlua.org') ");
Calling static methods:You can call static methods using only the class name and the.
notation from Lua.
state.DoString (@" local res4 = SomeClass.StaticMethod(4) ");
Calling properties:You can get (or set) any property using .
notation from Lua.
state.DoString (@" local res5 = obj.MyProperty ");
All methods, events or property need to be public available, NLua will fail to call non-public members.
If you are using Xamarin.iOS you need to Preserve
the class you want to use inside NLua, otherwise the Linker will remove the class from final binary if the class is not in use.
Sandboxing
There is many ways to sandbox scripts inside your application. I strongly recomend you to use plain Lua to do your sandbox.You can re-write theimport
function before load the user script and if the user try to import a .NET assembly nothing will happen.
state.DoString (@" import = function () end ");
Lua-Sandbox user-list
Copyright (c) 2014 Vinicius Jarina (viniciusjarina@gmail.com)
- NLua Example
- example
- example
- Example
- example
- Example
- Example
- Example
- Example
- example
- Example
- Example
- C#+NLua实现将Lua代码在主线程上执行
- TDD example
- CreateJobObject example
- tex example
- CreateThread example
- CreateJobObject example
- sql语句批量修改字段
- 自定义Dialog并实现接口回调
- Android 软键盘遮挡的四种解决方案
- ORACLE禁用启用触发器
- caffe 中 protobuffer
- NLua Example
- Android控件绘制完成原来是这样的
- HttpWebRequest的GetResponse或GetRequestStream偶尔超时 + 总结各种超时死掉的可能和相应的解决办法
- POJ 2230 Watchcow (欧拉回路模板)
- 怎样只用4行代码编写出一个从字符串到长整形的函数?
- java基础知识复习
- iOS开发在项目中添加文件夹的步骤
- 算法杂项
- MD5码加密