Handle unnamed functions better

This commit is contained in:
Deukhoofd 2018-12-09 17:49:54 +01:00
parent 5d7f155550
commit 875525b6be
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
2 changed files with 27 additions and 9 deletions

View File

@ -53,8 +53,17 @@ namespace Upsilon.BaseTypes.ScriptFunction
object result;
try
{
result = _object.GetType().InvokeMember(_method.Name, BindingFlags.InvokeMethod, UpsilonBinder.Default,
_object, objects.ToArray());
if (_object == null)
{
var array = objects.ToArray();
var methodInfo = _method.GetMethod(ref array);
result = methodInfo.Invoke(null, array);
}
else
{
result = _object.GetType().InvokeMember(_method.Name, BindingFlags.InvokeMethod, UpsilonBinder.Default,
_object, objects.ToArray());
}
}
catch (TargetInvocationException e)
{

View File

@ -73,28 +73,37 @@ namespace Upsilon.BaseTypes.UserData
return MethodParts;
}
public MethodInfo GetMethod(System.Type[] parameterTypes)
public MethodInfo GetMethod(ref object[] parameters)
{
foreach (var userDataMethodPart in MethodParts)
{
if (userDataMethodPart.Parameters.Length < parameterTypes.Length)
if (userDataMethodPart.Parameters.Length < parameters.Length)
continue;
bool valid = true;
for (var index = 0; index < userDataMethodPart.Parameters.Length; index++)
{
var userDataMethodParameter = userDataMethodPart.Parameters[index];
if (index >= parameterTypes.Length)
if (index >= parameters.Length)
{
if (userDataMethodParameter.IsOptional)
return userDataMethodPart.Method;
valid = false;
break;
}
var parameter = parameterTypes[index];
if (parameter.IsGenericType) parameter = parameter.GetGenericTypeDefinition();
if (userDataMethodParameter.Type != parameter &&
!userDataMethodParameter.Type.IsAssignableFrom(parameter))
var parameter = parameters[index];
var parameterType = parameter.GetType();
if (parameterType.IsGenericType) parameter = parameterType.GetGenericTypeDefinition();
if (userDataMethodParameter.Type != parameterType &&
!userDataMethodParameter.Type.IsAssignableFrom(parameterType))
{
if (parameter is ScriptType t)
{
if (userDataMethodParameter.Type.IsAssignableFrom(t.GetCSharpType()))
{
parameters[index] = ((ScriptType) parameter).ToCSharpObject();
continue;
}
}
valid = false;
break;
}