protected ModelParameters DetermineParameters(ModelAction action) { try { ModelParameters allparameters = action.Parameters; ModelParameters choosen = new ModelParameters(); //Loop through the method parameters foreach (ParameterInfo info in action.Method.GetParameters()) { //Find the all parameters assoicated with this param ModelParameters parameters = allparameters.Find(info.Name); //Exclude invalid parameters (if not requested) if (!this.InvalidParameters) { parameters = (ModelParameters)parameters.FindFlag((int)ModelItemFlags.Throws, false); } if (parameters.Count <= 0) { throw new ModelException(this, "Unable to find a ModelParameter for method parameter: '" + info.Name + "'"); } //Choose one of the parameters, based upon weight ModelParameter parameter = parameters.Choose(this); parameter.Accessed++; //Note: We cloning the param, since were choosing only one of the values to use this time. parameter = (ModelParameter)parameter.Clone(); //Choose (or generate) one of the values ModelValue value = DetermineParameterValue(parameter); value.Accessed++; //Add it to the array parameter.Value = value; choosen.Add(parameter); } return(choosen); } catch (Exception e) { //Make this easier to debug throw new ModelException(this, "DetermineParameters", e); } }
public virtual void OnException(ModelAction action, ModelParameters parameters, Exception e) { //Since were using reflection, if an error occurs within the method //make this easier to debug (for the method writer) so they see their exception //instead of the reflection based one. while (e.InnerException != null && e is TargetInvocationException) { e = e.InnerException; } //Find what should have thrown the exception, action or parameters Type exception = action.Exception; string exceptionid = action.ExceptionId; if (exception == null && exceptionid == null) { //Otherwise maybe one of the parameters was supposed to throw //Find the parameter that expects an error ModelParameters found = (ModelParameters)parameters.FindFlag((int)ModelItemFlags.Throws); if (found.Count > 0) { //Note: We find the 'highest' weighted parameter (ie: order of errors processed) found.SortByWeightDesc(); exception = found.First.Exception; exceptionid = found.First.ExceptionId; } } //Exception not expected if (exception == null && exceptionid == null) { throw new ModelException(action, "Threw an exception: " + e.Message, e); } //Expected: Simple verification, type based if (exception != null && (exception != e.GetType())) { throw new ModelException(action, "Threw the wrong exception: " + e.Message, e); } //Expected: Advanced verification, user implemented function if (exceptionid != null) { this.OnException(action, parameters, e, exceptionid); //Throws if not verified } }