Commit 593081fc authored by Vinicius Jarina's avatar Vinicius Jarina
Browse files

Fixed #297 LuaMethodWrapper.CallInvokeOnGenericMethod works with errors

parent 5c2d46fd
......@@ -148,10 +148,14 @@ namespace NLua.Method
try
{
object result;
if (method.IsConstructor)
_translator.Push(luaState, ((ConstructorInfo)method).Invoke(_lastCalledMethod.args));
result = ((ConstructorInfo)method).Invoke(_lastCalledMethod.args);
else
_translator.Push(luaState, method.Invoke(targetObject, _lastCalledMethod.args));
result = method.Invoke(targetObject, _lastCalledMethod.args);
_translator.Push(luaState, result);
}
catch (TargetInvocationException e)
{
......@@ -251,20 +255,30 @@ namespace NLua.Method
return 1;
}
if (_lastCalledMethod.cachedMethod.ContainsGenericParameters)
return CallInvokeOnGenericMethod(luaState, (MethodInfo)_lastCalledMethod.cachedMethod, targetObject);
return CallInvoke(luaState, _lastCalledMethod.cachedMethod, targetObject);
}
int CallInvokeOnGenericMethod(LuaState luaState, MethodInfo methodToCall, object targetObject)
{
_translator.MatchParameters(luaState, methodToCall, _lastCalledMethod, 0);
//need to make a concrete type of the generic method definition
var typeArgs = new List<Type>();
foreach (object arg in _lastCalledMethod.args)
typeArgs.Add(arg.GetType());
ParameterInfo [] parameters = methodToCall.GetParameters();
var concreteMethod = methodToCall.MakeGenericMethod(typeArgs.ToArray());
for (int i = 0; i < parameters.Length; i++)
{
ParameterInfo parameter = parameters[i];
if (!parameter.ParameterType.IsGenericParameter)
continue;
typeArgs.Add(_lastCalledMethod.args[i].GetType());
}
MethodInfo concreteMethod = methodToCall.MakeGenericMethod(typeArgs.ToArray());
_translator.Push(luaState, concreteMethod.Invoke(targetObject, _lastCalledMethod.args));
......@@ -317,6 +331,8 @@ namespace NLua.Method
return 1;
}
_translator.MatchParameters(luaState, methodToCall, _lastCalledMethod, 0);
return CallInvokeOnGenericMethod(luaState, (MethodInfo) methodToCall, targetObject);
}
......
......@@ -2514,6 +2514,28 @@ namespace NLuaTest
}
}
[Test]
public void CallMethodWithGeneric()
{
using (var lua = new Lua())
{
var u = new TestClassWithGenericMethod();
lua["u"] = u;
lua["foo"] = new DateTime(1234);
lua.DoString("u:GenericMethodWithCommonArgs(10, 11, foo)");
var foo = (DateTime)u.PassedValue;
int x = u.x;
int y = u.y;
Assert.AreEqual(foo, new DateTime(1234), "#1");
Assert.AreEqual(x, 10, "#2");
Assert.AreEqual(y, 11, "#3");
}
}
......
......@@ -6,6 +6,8 @@
public class TestClassWithGenericMethod
{
private object _PassedValue;
public int x;
public int y;
public object PassedValue
{
......@@ -25,6 +27,14 @@
_GenericMethodSuccess = true;
}
public void GenericMethodWithCommonArgs<U>(int x, int y, U value)
{
_PassedValue = value;
this.x = x;
this.y = y;
_GenericMethodSuccess = true;
}
internal bool Validate<T>(T value)
{
return value.Equals(_PassedValue);
......
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