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 ...@@ -148,10 +148,14 @@ namespace NLua.Method
try try
{ {
object result;
if (method.IsConstructor) if (method.IsConstructor)
_translator.Push(luaState, ((ConstructorInfo)method).Invoke(_lastCalledMethod.args)); result = ((ConstructorInfo)method).Invoke(_lastCalledMethod.args);
else else
_translator.Push(luaState, method.Invoke(targetObject, _lastCalledMethod.args)); result = method.Invoke(targetObject, _lastCalledMethod.args);
_translator.Push(luaState, result);
} }
catch (TargetInvocationException e) catch (TargetInvocationException e)
{ {
...@@ -251,20 +255,30 @@ namespace NLua.Method ...@@ -251,20 +255,30 @@ namespace NLua.Method
return 1; return 1;
} }
if (_lastCalledMethod.cachedMethod.ContainsGenericParameters)
return CallInvokeOnGenericMethod(luaState, (MethodInfo)_lastCalledMethod.cachedMethod, targetObject);
return CallInvoke(luaState, _lastCalledMethod.cachedMethod, targetObject); return CallInvoke(luaState, _lastCalledMethod.cachedMethod, targetObject);
} }
int CallInvokeOnGenericMethod(LuaState luaState, MethodInfo methodToCall, object 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 //need to make a concrete type of the generic method definition
var typeArgs = new List<Type>(); var typeArgs = new List<Type>();
foreach (object arg in _lastCalledMethod.args) ParameterInfo [] parameters = methodToCall.GetParameters();
typeArgs.Add(arg.GetType());
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)); _translator.Push(luaState, concreteMethod.Invoke(targetObject, _lastCalledMethod.args));
...@@ -317,6 +331,8 @@ namespace NLua.Method ...@@ -317,6 +331,8 @@ namespace NLua.Method
return 1; return 1;
} }
_translator.MatchParameters(luaState, methodToCall, _lastCalledMethod, 0);
return CallInvokeOnGenericMethod(luaState, (MethodInfo) methodToCall, targetObject); return CallInvokeOnGenericMethod(luaState, (MethodInfo) methodToCall, targetObject);
} }
......
...@@ -2514,7 +2514,29 @@ namespace NLuaTest ...@@ -2514,7 +2514,29 @@ 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 @@ ...@@ -6,6 +6,8 @@
public class TestClassWithGenericMethod public class TestClassWithGenericMethod
{ {
private object _PassedValue; private object _PassedValue;
public int x;
public int y;
public object PassedValue public object PassedValue
{ {
...@@ -25,6 +27,14 @@ ...@@ -25,6 +27,14 @@
_GenericMethodSuccess = true; _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) internal bool Validate<T>(T value)
{ {
return value.Equals(_PassedValue); 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