private object[] ConvertToTypedMethodArguments(InvokerContext context, MethodInfo method) { object[] arguments = { }; var parameters = method.GetParameters(); if (parameters.Length == 0) { return(arguments); } arguments = parameters.Select(p => CreateMethodParameterInstance(p.ParameterType, context.Parameter, p.Name)).ToArray(); return(arguments); }
public static InvokerContext Create(string binPath, string assembly, string @class, string method, string serialized = null) { var context = new InvokerContext { BinPath = binPath, Assembly = assembly, Type = @class, Method = method, Parameter = serialized != null?JsonConvert.DeserializeObject <dynamic>(serialized) : null }; context.ObjectInstance = ReflectionHelper.CreateComponentInstance(context); return(context); }
private MethodInfo GetMethod(InvokerContext context) { MethodInfo targetMethod = null; try { targetMethod = context.ObjectInstance.GetType().GetMethod(context.Method); } catch (Exception) { var methods = context.ObjectInstance.GetType().GetMethods(); foreach (var method in methods) { if (targetMethod != null) { break; } // omitted uncompilable code: ie. parameterless duplicated methods var parameters = method.GetParameters(); var parameterChecks = new bool[parameters.Length]; var index = 0; foreach (var parameter in parameters) { var value = context.Parameter[parameter.Name]; parameterChecks[index++] = value != null; if (value == null) { break; } } if (parameterChecks.Any(p => p == false)) { continue; } targetMethod = method; } } if (targetMethod == null) { throw new Exception(String.Format("Method [{0}] not found in Type <{1}>", context.Method, context.Type)); } return(targetMethod); }
private object InvokeTask(InvokerContext context) { var method = GetMethod(context); var arguments = ConvertToTypedMethodArguments(context, method); object result; try { result = method.Invoke(context.ObjectInstance, arguments); } catch (Exception ex) { throw new Exception(String.Format("Cannot invoke method [{0}]", method.Name), ex); } var safeResult = EncapsulateReturnValue(result, method.Name); return(safeResult); }
public async Task <object> Invoke(InvokerContext context) { return(await Task.Factory.StartNew(() => InvokeTask(context))); }