Skip to content

Commit

Permalink
Fixed a bug with invocating ILMethod through MethodInfo.Invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
liiir1985 committed Mar 12, 2020
1 parent ac03f54 commit e0a424a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
16 changes: 6 additions & 10 deletions ILRuntime/Runtime/Enviorment/CLRRedirections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -829,35 +829,31 @@ unsafe static class CLRRedirections
esp = ILIntepreter.PushObject(ret, mStack, obj);
else
esp = ret;
var ilmethod = ((ILRuntimeMethodInfo)instance).ILMethod;
if (p != null)
{
object[] arr = (object[])p;
foreach (var i in arr)
for(int i = 0; i < ilmethod.ParameterCount; i++)
{
esp = ILIntepreter.PushObject(esp, mStack, CheckCrossBindingAdapter(i));
esp = ILIntepreter.PushObject(esp, mStack, CheckCrossBindingAdapter(arr[i]));
}
}
bool unhandled;
var ilmethod = ((ILRuntimeMethodInfo)instance).ILMethod;
ret = intp.Execute(ilmethod, esp, out unhandled);
ILRuntimeMethodInfo imi = (ILRuntimeMethodInfo)instance;
var rt = imi.ReturnType;
var rt = imi.ILMethod.ReturnType;
if (rt != domain.VoidType)
{
var res = ret - 1;
if (res->ObjectType < ObjectTypes.Object)
{
if (rt is ILRuntimeWrapperType)
rt = ((ILRuntimeWrapperType)rt).CLRType.TypeForCLR;
if (rt is ILRuntimeType)
rt = ((ILRuntimeType)rt).ILType.TypeForCLR;
return ILIntepreter.PushObject(res, mStack, rt.CheckCLRTypes(StackObject.ToObject(res, domain, mStack)), true);
return ILIntepreter.PushObject(res, mStack, rt.TypeForCLR.CheckCLRTypes(StackObject.ToObject(res, domain, mStack)), true);
}
else
return ret;
}
else
return ret;
return ILIntepreter.PushNull(ret);
}
else
return ILIntepreter.PushObject(ret, mStack, ((MethodInfo)instance).Invoke(obj, (object[])p));
Expand Down
31 changes: 30 additions & 1 deletion TestCases/ReflectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ static void ReflectionTest11Sub(object o)
var p = o.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);//error
foreach (var i in p)
{
Console.WriteLine(i.GetValue(o, new object[] { 1, 2L ,3333}));
Console.WriteLine(i.GetValue(o, new object[] { 1, 2L, 3333 }));
i.SetValue(o, 333, new object[] { 123, 345L, 678 });
}
}
Expand Down Expand Up @@ -409,5 +409,34 @@ public sealed class TestController
{
public static TestController instance = new TestController();
}

public static void ReflectionTest16()
{
MethodInfo info = typeof(ReflectionTest).GetMethod(nameof(ReflectionTest16Sub), BindingFlags.NonPublic | BindingFlags.Static);
info.Invoke(null, new object[] { null });
info.Invoke(null, new object[] { null });
}

public static void ReflectionTest17()
{
MethodInfo info = typeof(ReflectionTest).GetMethod(nameof(ReflectionTest17Sub), BindingFlags.NonPublic | BindingFlags.Static);
int cnt = 0;
cnt += (int)info.Invoke(null, new object[] { null });
cnt += (int)info.Invoke(null, new object[] { null });

Console.WriteLine(cnt);
if (cnt != 40)
throw new Exception("Wrong result");
}

static void ReflectionTest16Sub(TestCases.TestCls t)
{
Console.WriteLine("OK");
}

static int ReflectionTest17Sub()
{
return 20;
}
}
}

0 comments on commit e0a424a

Please sign in to comment.