/// <summary> /// 动态获取索引值 /// </summary> /// <param name="container">对象</param> /// <param name="propIndex">索引</param> /// <returns>返回结果</returns> public object CallIndexValue(object container, object propIndex) { IList list; if (propIndex is int && (list = container as IList) != null) { return(list[(int)propIndex]); } IDictionary dic; if ((dic = container as IDictionary) != null) { return(dic[propIndex]); } if (propIndex is int && container is string) { return(((string)container)[(int)propIndex]); } Type t = container.GetType(); var info = DynamicHelpers.GetPropertyGetMethod(t, "Item"); if (info != null) { return(info.Invoke(container, new object[] { propIndex })); } return(null); }
/// <summary> /// 获取索引值 /// </summary> /// <param name="container">对象</param> /// <param name="propIndex">索引名称</param> /// <param name="isNumber">索引名称是否数字</param> /// <returns></returns> private object GetIndexedProperty(object container, bool isNumber, object propIndex) { IList list; if (isNumber && (list = container as IList) != null) { return(list[(int)propIndex]); } IDictionary dic; if ((dic = container as IDictionary) != null) { return(dic[propIndex]); } Type t = container.GetType(); var info = DynamicHelpers.GetPropertyGetMethod(t, "Item"); if (info != null) { return(info.Invoke(container, new object[] { propIndex })); } return(null); }