internal static string Eval(object[] source, StringBuffer expression, IFormatProvider provider) { if (StringBuffer.IsNullOrEmpty(expression)) { throw new ArgumentNullException("Expression cannot be null or empty."); } StringBuffer strIndex = expression.Copy(); object current = null; strIndex.SubstringBefore(',', ESearch.FirstOccurrence) .Fail(x => { x.SubstringBefore(':', ESearch.FirstOccurrence); }) .Do(x => { int intIndex; if (int.TryParse(x.ToString(), out intIndex)) { if ((intIndex >= 0) && (intIndex < source.Length)) { current = source[intIndex]; } else { // IndexOutOfRangeException throw new FormatException("Input string was not in a correct format."); } } }); string format = null; if (current != null) { // Index based if (expression.StartsWithAny(StringBuffer.GetWhitespaces(), true, false) || expression.EndsWithAny(StringBuffer.GetWhitespaces(), true, false)) { throw new FormatException("Input string was not in a correct format."); } format = expression.Copy() .SubstringAfter(',', ESearch.FirstOccurrence) .Fail(x => { x.SubstringAfter(':', ESearch.FirstOccurrence) .Fail(y => y.Clear()); }) .Prepend("{0:") .Append('}') .ToString(); } else { // Name based current = source[source.Length - 1]; if (expression.ContainsAny(StringBuffer.GetWhitespaces(), 0, expression.Length, false)) { throw new FormatException("Input string was not in a correct format."); } var expre = expression.Copy() .DoAtWhen(x => x.IndexOf(':'), (x, i) => (i > 0), (x, i) => { format = x.ToString(i + 1); x.Crop(0, i); }); //StringBuffer prop; do { /* * prop = expre.Copy() * .SubstringRange(0, '.', ESearch.FirstOccurrence) * .Fail(x => expre.Clear()) * .Succeed(x => expre.SubstringAfter('.', ESearch.FirstOccurrence)) * .DoWhenElse(x => x.Contains('['), * x => { current = Formatter.GetIndexedPropertyValue(current, x.ToString()); }, * x => { current = Formatter.GetPropertyValue(current, x.ToString()); }); */ expre.Copy() .SubstringRange(0, '.', ESearch.FirstOccurrence) .Fail(x => expre.Clear()) .Succeed(x => expre.SubstringAfter('.', ESearch.FirstOccurrence)) .DoWhenElse(x => x.Contains('['), x => { current = Formatter.GetIndexedPropertyValue(current, x.ToString()); }, x => { current = Formatter.GetPropertyValue(current, x.ToString()); }); }while ((expre.Length > 0) && (current != null)); if (current == null) { return(""); } if (format == null) { return(current.ToString()); } format = string.Concat("{0:" + format + "}"); } if (provider != null) { return(string.Format(provider, format, current)); } return(string.Format(format, current)); }