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

@ -52,10 +52,19 @@ namespace Upsilon.BaseTypes.ScriptFunction
objects.AddRange(variables.Select(x => (object) x).ToList() ); objects.AddRange(variables.Select(x => (object) x).ToList() );
object result; object result;
try try
{
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, result = _object.GetType().InvokeMember(_method.Name, BindingFlags.InvokeMethod, UpsilonBinder.Default,
_object, objects.ToArray()); _object, objects.ToArray());
} }
}
catch (TargetInvocationException e) catch (TargetInvocationException e)
{ {
if (e.InnerException != null) throw e.InnerException; if (e.InnerException != null) throw e.InnerException;

View File

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