internal static object Execute(ViewModel vm, MethodInfo method, object[] parameters) { return(vm.OnExecute(method, parameters)); }
internal static string GetClientScript(ViewModel vm) { return(vm.ClientScript.ToString()); }
internal static void CallOnCreated(ViewModel vm) { vm.OnCreated(); }
internal static void SetData(ViewModel vm, JObject data) { vm.Data = data; }
/// <summary> /// Find a method in all componenets and execute if found /// </summary> private object ExecuteMethod(string name, ViewModel vm, JToken[] parameters, IFormFileCollection files, IServiceProvider serviceProvider) { var met = _component.Methods[name]; var method = met.Method; var pars = new List <object>(); var index = 0; // check for permissions if (met.IsAuthenticated && _user.Identity.IsAuthenticated == false) { throw new HttpException(401); } if (met.Roles.Length > 0 && met.Roles.Any(x => _user.IsInRole(x)) == false) { throw new HttpException(403, $"Forbidden. This method requires all this roles: `{string.Join("`, `", met.Roles)}`"); } // convert each parameter as declared method in type foreach (var p in method.GetParameters()) { // if has no passed parameter, try create instance based on DI if (index >= parameters.Length) { var value = serviceProvider.GetService(p.ParameterType); pars.Add(value); continue; } var token = parameters[index++]; if (p.ParameterType == typeof(IFormFile)) { var value = ((JValue)token).Value.ToString(); pars.Add(files.GetFile(value)); } else if (p.ParameterType == typeof(IList <IFormFile>)) { var value = ((JValue)token).Value.ToString(); pars.Add(files.GetFiles(value)); } else if (token.Type == JTokenType.Object) { var obj = ((JObject)token).ToObject(p.ParameterType); pars.Add(obj); } else if (token.Type == JTokenType.String && p.ParameterType.IsEnum) { var value = ((JValue)token).Value.ToString(); pars.Add(Enum.Parse(p.ParameterType, value)); } else { var value = ((JValue)token).Value; pars.Add(Convert.ChangeType(value, p.ParameterType)); } } // now execute method inside viewmodel return(ViewModel.Execute(vm, method, pars.ToArray())); }