public static object SendNativeMessage(Machine machine, object obj, string mthname, object[] args)
{
if (args == null || args.Length == 0)
{
if (unaryMethods.ContainsKey(mthname))
{
IMethod unimethod = unaryMethods[mthname];
return unimethod.ExecuteNative(machine, obj, args);
}
}
else if (args.Length == 1)
{
if (binaryMethods.ContainsKey(mthname))
{
IMethod binmethod = binaryMethods[mthname];
return binmethod.ExecuteNative(machine, obj, args);
}
}
else if (args.Length == 2)
{
if (ternaryMethods.ContainsKey(mthname))
{
IMethod ternarymethod = ternaryMethods[mthname];
return ternarymethod.ExecuteNative(machine, obj, args);
}
}
else if (args.Length == 3)
{
if (cuaternaryMethods.ContainsKey(mthname))
{
IMethod cuaternarymethod = cuaternaryMethods[mthname];
return cuaternarymethod.ExecuteNative(machine, obj, args);
}
}
if (obj is Type)
return SendNativeStaticMessage((Type)obj, mthname, args);
// TODO support for indexed properties
Type type = obj.GetType();
if (args != null && args.Length > 0)
{
PropertyInfo prop = type.GetProperty(mthname, System.Reflection.BindingFlags.SetProperty | System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
if (prop != null)
return type.InvokeMember(mthname, System.Reflection.BindingFlags.SetProperty | System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, null, obj, args);
}
try
{
return obj.GetType().InvokeMember(mthname, System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, null, obj, args);
}
catch (System.Reflection.TargetInvocationException ex)
{
throw ex.InnerException;
}
}