/// <devdoc> /// </devdoc> private static object Eval(object container, string[] expressionParts) { Debug.Assert((expressionParts != null) && (expressionParts.Length != 0), "invalid expressionParts parameter"); object prop; int i; for (prop = container, i = 0; (i < expressionParts.Length) && (prop != null); i++) { string expr = expressionParts[i]; bool indexedExpr = expr.IndexOfAny(indexExprStartChars) >= 0; if (indexedExpr == false) { prop = DataBinder.GetPropertyValue(prop, expr); } else { prop = DataBinder.GetIndexedPropertyValue(prop, expr); } } return(prop); }
/// <devdoc> /// </devdoc> public static string GetPropertyValue(object container, string propName, string format) { object value = DataBinder.GetPropertyValue(container, propName); if ((value == null) || (value == System.DBNull.Value)) { return(string.Empty); } else { if (String.IsNullOrEmpty(format)) { return(value.ToString()); } else { return(string.Format(format, value)); } } }
/// <devdoc> /// </devdoc> public static object GetIndexedPropertyValue(object container, string expr) { if (container == null) { throw new ArgumentNullException("container"); } if (String.IsNullOrEmpty(expr)) { throw new ArgumentNullException("expr"); } object prop = null; bool intIndex = false; int indexExprStart = expr.IndexOfAny(indexExprStartChars); int indexExprEnd = expr.IndexOfAny(indexExprEndChars, indexExprStart + 1); if ((indexExprStart < 0) || (indexExprEnd < 0) || (indexExprEnd == indexExprStart + 1)) { throw new ArgumentException();//UNDONE } string propName = null; object indexValue = null; string index = expr.Substring(indexExprStart + 1, indexExprEnd - indexExprStart - 1).Trim(); if (indexExprStart != 0) { propName = expr.Substring(0, indexExprStart); } if (index.Length != 0) { if (((index[0] == '"') && (index[index.Length - 1] == '"')) || ((index[0] == '\'') && (index[index.Length - 1] == '\''))) { indexValue = index.Substring(1, index.Length - 2); } else { if (Char.IsDigit(index[0])) { // treat it as a number int parsedIndex; intIndex = Int32.TryParse(index, NumberStyles.Integer, CultureInfo.InvariantCulture, out parsedIndex); if (intIndex) { indexValue = parsedIndex; } else { indexValue = index; } } else { // treat as a string indexValue = index; } } } if (indexValue == null) { throw new ArgumentException();//UNDONE } object collectionProp = null; if ((propName != null) && (propName.Length != 0)) { collectionProp = DataBinder.GetPropertyValue(container, propName); } else { collectionProp = container; } if (collectionProp != null) { Array arrayProp = collectionProp as Array; if (arrayProp != null && intIndex) { prop = arrayProp.GetValue((int)indexValue); } else if ((collectionProp is IList) && intIndex) { prop = ((IList)collectionProp)[(int)indexValue]; } else { PropertyInfo propInfo = collectionProp.GetType().GetProperty("Item", BindingFlags.Public | BindingFlags.Instance, null, null, new Type[] { indexValue.GetType() }, null); if (propInfo != null) { prop = propInfo.GetValue(collectionProp, new object[] { indexValue }); } else { throw new ArgumentException();//UNDONE } } } return(prop); }
/// <include file='doc\DataBinder.uex' path='docs/doc[@for="DataBinder.GetIndexedPropertyValue"]/*' /> /// <devdoc> /// </devdoc> public static object GetIndexedPropertyValue(object container, string expr) { if (container == null) { throw new ArgumentNullException("container"); } if ((expr == null) || (expr.Length == 0)) { throw new ArgumentNullException("expr"); } object prop = null; bool intIndex = false; int indexExprStart = expr.IndexOfAny(indexExprStartChars); int indexExprEnd = expr.IndexOfAny(indexExprEndChars, indexExprStart + 1); if ((indexExprStart < 0) || (indexExprEnd < 0) || (indexExprEnd == indexExprStart + 1)) { throw new ArgumentException(HttpRuntime.FormatResourceString(SR.DataBinder_Invalid_Indexed_Expr, expr)); } string propName = null; object indexValue = null; string index = expr.Substring(indexExprStart + 1, indexExprEnd - indexExprStart - 1).Trim(); if (indexExprStart != 0) { propName = expr.Substring(0, indexExprStart); } if (index.Length != 0) { if (((index[0] == '"') && (index[index.Length - 1] == '"')) || ((index[0] == '\'') && (index[index.Length - 1] == '\''))) { indexValue = index.Substring(1, index.Length - 2); } else { if (Char.IsDigit(index[0])) { // treat it as a number try { indexValue = Int32.Parse(index, CultureInfo.InvariantCulture); intIndex = true; } catch (Exception) { // fall back on string indexValue = index; } } else { // treat as a string indexValue = index; } } } if (indexValue == null) { throw new ArgumentException(HttpRuntime.FormatResourceString(SR.DataBinder_Invalid_Indexed_Expr, expr)); } object collectionProp = null; if ((propName != null) && (propName.Length != 0)) { collectionProp = DataBinder.GetPropertyValue(container, propName); } else { collectionProp = container; } if (collectionProp != null) { if ((collectionProp is Array) && intIndex) { prop = ((object[])collectionProp)[(int)indexValue]; } else if ((collectionProp is IList) && intIndex) { prop = ((IList)collectionProp)[(int)indexValue]; } else { PropertyInfo propInfo = collectionProp.GetType().GetProperty("Item", BindingFlags.Public | BindingFlags.Instance, null, null, new Type[] { indexValue.GetType() }, null); if (propInfo != null) { prop = propInfo.GetValue(collectionProp, new object[] { indexValue }); } else { throw new ArgumentException(HttpRuntime.FormatResourceString(SR.DataBinder_No_Indexed_Accessor, collectionProp.GetType().FullName)); } } } return(prop); }