我在一个C#项目中使用NLUA。我使用以下代码注册一个C#方法,以便在LUA (NLUA)环境中可用(和工作):
// C# code to register the methoin LUA environment:
Lua state = new Lua();
state["MyLog"] = new LuaLog();
// C# Class and Method:
public class LuaLog {
public void write(string aLog) {
LogManager.addLog(aLog);
}
}
--LUA CODE TO CALL C# METHOD:
MyLog:write("This is a log string")好吧,我想调用"MyLog:write()“,但是传递一个表,而不是一个字符串。例如:
MyLog:write( {LogText="This is a log string", LogType="INFO"} )有可能吗?如何编写C#方法将该参数作为表读取?我试过:
// C# CODE:
public void write(Dictionary<string, string> aLog)
public void write(List<string> aLog)但什么都不管用。你能帮帮我吗?
谢谢!
发布于 2022-03-30 10:49:41
实际例子:
public class Example {
public Dictionary<string, string> MethodCalledFromLua(Object aInput) {
//
// READ LUA ARGUMENTS WITH THE PROPER TYPE
//
if(aInput is LuaTable) {
LuaTable theLuaTable = aLuaTable as LuaTable;
Console.WriteLine("LUA TABLE: " + theLuaTable["name"] + " --> " + theLuaTable["surname"]);
}
if(aInput is string) {
string theString = aInput as string;
Console.WriteLine("STRING: " + theString);
}
//
// RETURN A DICTIONARY TO LUA (NLUA)
// (remember that LUA is case-sensitive!)
//
Dictionary<string, string> ret = new Dictionary<string, string>();
Dictionary<string, object> editor = FormsManager.newEditor();
ret["DATA_1"] = "My Name";
ret["DATA_2"] = "My Last Name";
return ret;
}
}从C#注册NLUA类:
luaState["MyExample"] = new Example();LUA代码:
local ret = MyExample:MethodCalledFromLua( {name = "the name", surname = "the surname" } )
print(ret.DATA_1)https://stackoverflow.com/questions/71661096
复制相似问题