public object Evaluate(params object[] parameters) { object source = parameters != null ? parameters[0] : null; string key = (string)parameters[1]; object condition = parameters[2]; if (source == null) { return(null); } if (key == null) { throw FunctionEvaluationException.WrongParameter(this, 1, source); } if (source is IEnumerable) { var linq = ((IEnumerable)source).Cast <object>(); return(linq.Where( item => { var t = new Reflection(item)[key]; if (t == null && condition == null) { return true; } return Equals(TypeConverter.To(t, condition.GetType()), condition); } ).ToArray()); } throw FunctionEvaluationException.WrongParameter(this, 0, source); }
public object Evaluate(params object[] parameters) { object source = parameters != null ? parameters[0] : null; if (source == null) { return(0); } else if (source is string) { return(((string)source).Length); } else if (source is IDictionary) { return(((IDictionary)source).Count); } else if (source is Array) { return(((Array)source).Length); } else if (source is IList) { return(((IList)source).Count); } if (source is IEnumerable) { return(CountEnumerable((IEnumerable)source)); } else { throw FunctionEvaluationException.WrongParameter(this, 0, source); } }
public object Evaluate(params object[] parameters) { object source = parameters != null ? parameters[0] : null; if (source == null) { return(null); } else if (source is string) { return(StringUtils.Reverse((string)source)); } else if (source is Array) { return(CollectionUtils.Reverse((Array)source)); } else if (source is IEnumerable) { Stack reversed = new Stack(); foreach (var o in source as IEnumerable) { reversed.Push(o); } return(reversed); } else { throw FunctionEvaluationException.WrongParameter(this, 0, source); } }
public Function Obtain(string functionName) { IFunctionDefinition definition; functions.TryGetValue(functionName, out definition); if (definition == null) { throw FunctionEvaluationException.UnkownFunction(functionName); } return(new Function(definition)); }
public override object Evaluate(IModel model) { var parameters = new List <object>(); for (int i = 0; i < _function.Arguments.Length; i++) { var arg = _function.Arguments[i]; if (!arg.Params) { var node = _nested.Nodes[i]; object value = node.Evaluate(model); parameters.Add(TypeConverter.To(value, _function.Arguments[i].Type)); } else { for (int pi = i; pi < _nested.Nodes.Count; pi++) { var node = _nested.Nodes[pi]; object value = node.Evaluate(model); parameters.Add(TypeConverter.To(value, _function.Arguments[i].Type)); } } } try { return(_function.Evaluate(parameters.ToArray())); } catch (ExceptionWithContext EWC) { if (EWC.Context == null) { throw ExceptionWithContext.MakePartial(EWC).Decorate(Token); } else { throw EWC; } } catch (Exception e) { throw FunctionEvaluationException.EvaluationError(e).Decorate(Token); } }
public object Evaluate(params object[] parameters) { object source = parameters != null ? parameters[0] : null; string key = (string)parameters[1]; if (source == null) { return(null); } if (key == null) { throw FunctionEvaluationException.WrongParameter(this, 1, source); } if (source is IEnumerable) { var linq = ((IEnumerable)source).Cast <object>(); return(linq.Select( item => new Reflection(item)[key] ).ToArray()); } throw FunctionEvaluationException.WrongParameter(this, 0, source); }