2012-04-08 223 views
0

我使用LuaInterface爲我想在Lua中提供的某些對象註冊getter。 E.G:LuaInterface:訪問對象屬性

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?

回答

0

您可能會發現這個代碼在瞭解瞭如何訪問屬性的有用:

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