LuaInterface:访问对象属性

我正在使用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

我该如何访问返回的公共对象属性?这是可能的吗,还是我必须为每个对象属性编写getter?

原文链接 https://stackoverflow.com/questions/10063184

点赞
stackoverflow用户151501
stackoverflow用户151501

你可能会发现以下代码有助于理解如何访问属性:

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"));
}

然后在 Lua 中:

print(dump(getObjAt(3)))
2012-04-10 16:41:25