Lua接口:访问对象属性
本文关键字:对象 属性 访问 接口 Lua | 更新日期: 2023-09-27 18:30:21
我正在使用LuaInterface为一些我想在Lua中可用的对象注册一个getter。例如:
public MyObject getObjAt(int index)
{
return _myObjects[index];
}
我的Lua文件:
obj = getObjAt(3)
print(obj.someProperty) // Prints "someProperty"
print(obj.moooo) // Prints "moooo"
print(obj:someMethod()) // Works fine, method is being executed
在 Lua 中返回公共对象属性后,如何准确访问它们?这甚至可能还是我必须为每个对象属性编写 getter?
您可能会发现此代码在了解如何访问属性时很有用:
class Lister
{
public string ListObjectMembers(Object o)
{
var result = new StringBuilder();
ProxyType proxy = o as ProxyType;
Type type = proxy != null ? proxy.UnderlyingSystemType : o.GetType();
result.AppendLine("Type: " + type);
result.AppendLine("Properties:");
foreach (PropertyInfo propertyInfo in type.GetProperties())
result.AppendLine(" " + propertyInfo.Name);
result.AppendLine("Methods:");
foreach (MethodInfo methodInfo in type.GetMethods())
result.AppendLine(" " + methodInfo.Name);
return result.ToString();
}
}
并注册函数:
static Lister _lister = new Lister();
private static void Main() {
Interpreter = new Lua();
Interpreter.RegisterFunction("dump", _lister,
_lister.GetType().GetMethod("ListObjectMembers"));
}
然后在卢阿:
print(dump(getObjAt(3)))