【问题标题】:LuaInterface: Access objects propertiesLuaInterface:访问对象属性
【发布时间】:2012-04-11 00:41:25
【问题描述】:

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

【问题讨论】:

    标签: c# lua luainterface


    【解决方案1】:

    您可能会发现这段代码有助于理解如何访问属性:

    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)))
    

    【讨论】:

      猜你喜欢
      • 2011-05-09
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多