public void ArrayBindingWithEmptyNode() { var node = new CompositeNode("unnamed"); Assert.AreEqual(new String[0], binder.BindParameter(typeof(String[]), "name", node)); Assert.AreEqual(new String[0], binder.BindParameter(typeof(String[]), "name", node)); Assert.AreEqual(new int[0], binder.BindParameter(typeof(int[]), "name", node)); Assert.AreEqual(new int[0], binder.BindParameter(typeof(int[]), "name", node)); }
/// <summary> /// Returns an array that hopefully fills the arguments of the selected action. /// </summary> /// <remarks> /// Each parameter is inspected and we try to obtain an implementation of /// <see cref="IParameterBinder"/> from the attributes the parameter have (if any). /// If an implementation is found, it's used to fill the value for that parameter. /// Otherwise we use simple conversion to obtain the value. /// </remarks> /// <param name="parameters">Parameters to obtain the values to</param> /// <param name="request">The current request, which is the source to obtain the data</param> /// <param name="actionArgs">Extra arguments to pass to the action.</param> /// <returns>An array with the arguments values</returns> protected virtual object[] BuildMethodArguments(ParameterInfo[] parameters, IRequest request, IDictionary <string, object> actionArgs) { var args = new object[parameters.Length]; var paramName = String.Empty; var value = String.Empty; try { for (var argIndex = 0; argIndex < args.Length; argIndex++) { // // If the parameter is decorated with an attribute // that implements IParameterBinder, it's up to it // to convert itself // var param = parameters[argIndex]; paramName = GetRequestParameterName(param); var handled = false; var attributes = param.GetCustomAttributes(false); foreach (var attr in attributes) { var paramBinder = attr as IParameterBinder; if (paramBinder != null) { args[argIndex] = paramBinder.Bind(Context, this, ControllerContext, param); handled = true; break; } } // // Otherwise we handle it // if (!handled) { object convertedVal = null; var conversionSucceeded = false; if (actionArgs != null && actionArgs.ContainsKey(paramName)) { var actionArg = actionArgs[paramName]; var actionArgType = actionArg != null?actionArg.GetType() : param.ParameterType; convertedVal = binder.Converter.Convert(param.ParameterType, actionArgType, actionArg, out conversionSucceeded); } if (!conversionSucceeded) { convertedVal = binder.BindParameter(param.ParameterType, paramName, Request.ParamsNode); } args[argIndex] = convertedVal; } } } catch (FormatException ex) { throw new MonoRailException( String.Format("Could not convert {0} to request type. " + "Argument value is '{1}'", paramName, Params.Get(paramName)), ex); } catch (Exception ex) { throw new MonoRailException( String.Format("Error building method arguments. " + "Last param analyzed was {0} with value '{1}'", paramName, value), ex); } return(args); }