-
Notifications
You must be signed in to change notification settings - Fork 1
.NET Objects
Kevin Zhao edited this page Jan 25, 2018
·
6 revisions
.NET objects can be passed directly into Lua:
var list = new List<int>();
lua["list"] = list;
You can then use them transparently like normal .NET objects:
lua.DoString(@"
list:Add(1)
list:Add(4)
list:Add(9)
c = list.Count");
You can call generic methods with the following syntax:
public class Test {
public T Method<T>(T t);
}
lua["test"] = new Test();
lua.ImportType(typeof(int));
lua.DoString("result = test:Method(Int32)(7)");
You can access indexed properties with the following syntax:
lua["list"] = new List<int>();
lua.DoString(@"
function incfirst()
local first = list.Item:Get(0)
list.Item:Set(first + 1, 0)
end");
This is rather cumbersome, however, because many collections rely on an indexed Item
property. An elegant solution would involve using native Lua tables.
You can access events with the signature (object, EventArgs)
with the following syntax:
class Test {
public event EventHandler Event;
}
lua["test"] = new Test();
lua.DoString("callback = function(obj, args) print(obj) end)");
lua.DoString("test.Event:Add(callback)");
lua.DoString("test.Event:Remove(callback)");
This is highly unrecommended, however! You have no control over when the event is called. It could be called on a different thread, even, which would lead to disaster since Lua is not thread-safe.
Members of a type can be hidden using the LuaIgnore
attribute.