private int getMethodInternal(LuaCore.lua_State luaState) { object obj = translator.getRawNetObject(luaState, 1); if (!translator.interpreter.ManagedObjectSecurityPolicy.PermitAccessToObject(obj)) { LuaLib.lua_pushnil(luaState); return(1); } if (obj.IsNull()) { translator.throwError(luaState, "trying to index an invalid object reference"); LuaLib.lua_pushnil(luaState); return(1); } object index = translator.getObject(luaState, 2); //var indexType = index.GetType(); string methodName = index as string; // will be null if not a string arg var objType = obj.GetType(); // Handle the most common case, looking up the method by name. // CP: This will fail when using indexers and attempting to get a value with the same name as a property of the object, // ie: xmlelement['item'] <- item is a property of xmlelement try { if (!methodName.IsNull() && isMemberPresent(objType, methodName)) { return(getMember(luaState, objType, obj, methodName, BindingFlags.Instance | BindingFlags.IgnoreCase)); } } catch { } // Try to access by array if the type is right and index is an int (lua numbers always come across as double) if (objType.IsArray && index is double) { int intIndex = (int)((double)index); if (objType.UnderlyingSystemType == typeof(float[])) { float[] arr = ((float[])obj); translator.push(luaState, arr [intIndex]); } else if (objType.UnderlyingSystemType == typeof(double[])) { double[] arr = ((double[])obj); translator.push(luaState, arr [intIndex]); } else if (objType.UnderlyingSystemType == typeof(int[])) { int[] arr = ((int[])obj); translator.push(luaState, arr [intIndex]); } else { object[] arr = (object[])obj; translator.push(luaState, arr [intIndex]); } } else { // Try to use get_Item to index into this .net object var methods = objType.GetMethods(); foreach (var mInfo in methods) { if (mInfo.Name == "get_Item") { //check if the signature matches the input if (mInfo.GetParameters().Length == 1) { var getter = mInfo; var actualParms = (!getter.IsNull()) ? getter.GetParameters() : null; if (actualParms.IsNull() || actualParms.Length != 1) { translator.throwError(luaState, "method not found (or no indexer): " + index); LuaLib.lua_pushnil(luaState); } else { // Get the index in a form acceptable to the getter index = translator.getAsType(luaState, 2, actualParms [0].ParameterType); object[] args = new object[1]; // Just call the indexer - if out of bounds an exception will happen args [0] = index; try { object result = getter.Invoke(obj, args); translator.push(luaState, result); } catch (TargetInvocationException e) { // Provide a more readable description for the common case of key not found if (e.InnerException is KeyNotFoundException) { translator.throwError(luaState, "key '" + index + "' not found "); } else { translator.throwError(luaState, "exception indexing '" + index + "' " + e.Message); } LuaLib.lua_pushnil(luaState); } } } } } } LuaLib.lua_pushboolean(luaState, false); return(2); }