Unverified Commit ae588db4 authored by Vinicius Jarina's avatar Vinicius Jarina Committed by GitHub
Browse files

Fixed #324 How to access index property of IDictionary<T, K> (#325)

parent 62fd5c11
...@@ -484,7 +484,7 @@ namespace NLua ...@@ -484,7 +484,7 @@ namespace NLua
if (methodInfo.GetParameters().Length != 1) if (methodInfo.GetParameters().Length != 1)
continue; continue;
var actualParams = methodInfo.GetParameters(); ParameterInfo[] actualParams = methodInfo.GetParameters();
if (actualParams.Length != 1) if (actualParams.Length != 1)
{ {
...@@ -495,6 +495,11 @@ namespace NLua ...@@ -495,6 +495,11 @@ namespace NLua
{ {
// Get the index in a form acceptable to the getter // Get the index in a form acceptable to the getter
index = _translator.GetAsType(luaState, 2, actualParams[0].ParameterType); index = _translator.GetAsType(luaState, 2, actualParams[0].ParameterType);
// If the index type and the parameter doesn't match, just skip it
if (index == null)
break;
object[] args = new object[1]; object[] args = new object[1];
// Just call the indexer - if out of bounds an exception will happen // Just call the indexer - if out of bounds an exception will happen
...@@ -504,6 +509,7 @@ namespace NLua ...@@ -504,6 +509,7 @@ namespace NLua
{ {
object result = methodInfo.Invoke(obj, args); object result = methodInfo.Invoke(obj, args);
_translator.Push(luaState, result); _translator.Push(luaState, result);
return 1;
} }
catch (TargetInvocationException e) catch (TargetInvocationException e)
{ {
......
...@@ -15,6 +15,7 @@ using NUnit.Framework; ...@@ -15,6 +15,7 @@ using NUnit.Framework;
using Lua = NLua.Lua; using Lua = NLua.Lua;
using LuaFunction = NLua.LuaFunction; using LuaFunction = NLua.LuaFunction;
using System.Diagnostics; using System.Diagnostics;
using System.Collections.Generic;
// ReSharper disable StringLiteralTypo // ReSharper disable StringLiteralTypo
...@@ -2550,7 +2551,26 @@ namespace NLuaTest ...@@ -2550,7 +2551,26 @@ namespace NLuaTest
} }
} }
[Test]
public void CallDictionary()
{
using (var lua = new Lua())
{
var obj = new Dictionary<string, string>()
{
{ "key1" ,"value1" },
{ "key2" ,"value2" }
};
lua["obj"] = obj;
lua.DoString("i = obj.key1");
lua.DoString("j = obj['key2']");
Assert.AreEqual("value1", lua["i"], "#1");
Assert.AreEqual("value2", lua["j"], "#2");
}
}
static Lua m_lua; static Lua m_lua;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment