Work on calling CSharp methods

This commit is contained in:
2018-12-11 15:50:24 +01:00
parent efea92d32a
commit e57129e116
6 changed files with 100 additions and 25 deletions

View File

@@ -25,7 +25,7 @@ namespace Upsilon.BaseTypes.UserData
foreach (var match in matches)
{
if (IsValidMatch(match, ref args))
if (IsValidMatch(match, args.Select(x => x.GetType()).ToArray()))
return match;
}
@@ -33,27 +33,26 @@ namespace Upsilon.BaseTypes.UserData
return null;
}
public bool IsValidMatch(MethodBase match, ref object[] args)
public bool IsValidMatch(MethodBase match, System.Type[] argumentTypes)
{
var parameters = match.GetParameters();
for (var i = 0; i < parameters.Length; i++)
{
var matchParameter = parameters[i];
if (args.Length <= i)
if (argumentTypes.Length <= i)
{
if (matchParameter.IsOptional)
return true;
return false;
}
var argument = args[i];
var argumentType = argument.GetType();
var argumentType = argumentTypes[i];
var typeCode = System.Type.GetTypeCode(matchParameter.ParameterType);
switch (typeCode)
{
case TypeCode.Boolean:
if (argument is ScriptBoolean)
if (argumentType == typeof(ScriptBoolean))
{
continue;
}
@@ -61,7 +60,7 @@ namespace Upsilon.BaseTypes.UserData
break;
case TypeCode.Char:
case TypeCode.String:
if (argument is ScriptString)
if (argumentType == typeof(ScriptString))
{
continue;
}
@@ -78,7 +77,7 @@ namespace Upsilon.BaseTypes.UserData
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
if (argument is ScriptNumber)
if (typeof(ScriptNumber).IsAssignableFrom(argumentType))
{
continue;
}
@@ -88,14 +87,6 @@ namespace Upsilon.BaseTypes.UserData
continue;
}
if (!typeof(ScriptType).IsAssignableFrom(matchParameter.ParameterType) &&
argument is IUserData ud)
{
var csharpType = ud.GetCSharpType();
if (matchParameter.ParameterType.IsAssignableFrom(csharpType))
continue;
}
if (argumentType == typeof(ScriptNull))
{
continue;
@@ -195,7 +186,20 @@ namespace Upsilon.BaseTypes.UserData
if (typeof(IList).IsAssignableFrom(type))
{
if (value is ListUserData d)
return d.List;
{
if (d.List.GetType() == type)
return d.List;
var generics = type.GetGenericArguments();
var l1 = typeof(List<>);
var requiredListType = l1.MakeGenericType(generics[0]);
var list = (IList)Activator.CreateInstance(requiredListType);
foreach (var variable in d.List)
{
list.Add(ChangeType(variable, requiredListType, culture));
}
return list;
}
if (value is ScriptTable.ScriptTable table)
{
var generics = type.GetGenericArguments();
@@ -262,8 +266,15 @@ namespace Upsilon.BaseTypes.UserData
public override MethodBase SelectMethod(BindingFlags bindingAttr, MethodBase[] matches, System.Type[] types, ParameterModifier[] modifiers)
{
throw new System.NotImplementedException();
foreach (var match in matches)
{
if (IsValidMatch(match, types))
{
return match;
}
}
return null;
}
public override PropertyInfo SelectProperty(BindingFlags bindingAttr, PropertyInfo[] match, System.Type returnType, System.Type[] indexes,

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Upsilon.Evaluator;
using Upsilon.StandardLibraries;
@@ -78,7 +79,7 @@ namespace Upsilon.BaseTypes.UserData
foreach (var userDataMethodPart in MethodParts)
{
var methodBase = userDataMethodPart.Method;
if (UpsilonBinder.Default.IsValidMatch(methodBase, ref arguments))
if (UpsilonBinder.Default.IsValidMatch(methodBase, arguments.Select(x => x.GetType()).ToArray()))
{
var parameters = methodBase.GetParameters();
for (var i = 0; i < parameters.Length; i++)