/// <summary> /// resolves a value from a set of objects base on a reference that contains the key to the object in the dictionary /// and optionally a dot notation reference to properties of the object. /// </summary> /// <param name="indexedRefStart">an indexed reference start string such as [ in some.reference[index]</param> /// <param name="indexedRefEnd">an indexed reference start string such as ] in some.reference[index]</param> /// <param name="objects"></param> /// <param name="refName"></param> /// <returns></returns> public static object GetObjectFromReference( string indexedRefStart, string indexedRefEnd, Dictionary <string, object> objects, string refName ) { object refValue = null; int firstIndexOfDot = refName.IndexOf("."); int firstIndexOfSquareBracket = refName.IndexOf(indexedRefStart); if (StringUtility.Contains(refName, ".") && (firstIndexOfSquareBracket < 0 || firstIndexOfDot < firstIndexOfSquareBracket)) { StringTokenizer tokenizer = new StringTokenizer(refName, "."); string envName = tokenizer.NextToken(); object obj = null; if (StringUtility.NotEmpty(envName)) { obj = objects[envName]; if (obj != null) { while (tokenizer.HasMoreTokens()) { if (obj == null) { break; } string token = tokenizer.NextToken(); string propName = token; if (StringUtility.NotEmpty(token)) { bool hasIndex = false; if (StringUtility.Contains(token, indexedRefStart)) { hasIndex = true; propName = token.Substring(0, token.IndexOf(indexedRefStart)); } PropertyInfo prop = obj.GetType().GetProperty(propName); if (prop != null && prop.CanRead) { if (typeof(IDictionary).IsAssignableFrom(prop.PropertyType)) { IDictionary dictObj = (IDictionary)prop.GetValue(obj, null); if (hasIndex) { obj = dictObj[StringUtility.ExtractFromDelimiters(token, indexedRefStart, indexedRefEnd)]; } else { obj = dictObj; } } else if (typeof(IList).IsAssignableFrom(prop.PropertyType)) { IList listObj = (IList)prop.GetValue(obj, null); if (hasIndex) { try { int indx = Convert.ToInt32(StringUtility.ExtractFromDelimiters(token, indexedRefStart, indexedRefEnd)); obj = listObj[indx]; } catch { obj = listObj; } } else { obj = listObj; } } else { obj = prop.GetValue(obj, null); } } else { obj = null; break; } } } } } refValue = obj; } else { bool hasIndex = false; string propName = refName; if (StringUtility.Contains(refName, indexedRefStart)) { hasIndex = true; propName = refName.Substring(0, refName.IndexOf(indexedRefStart)); } object obj = objects[propName]; if (obj == null) { refValue = null; } else { if (hasIndex) { if (typeof(IDictionary).IsAssignableFrom(obj.GetType())) { IDictionary dictObj = (IDictionary)obj; obj = dictObj[StringUtility.ExtractFromDelimiters(refName, indexedRefStart, indexedRefEnd)]; } else if (typeof(IList).IsAssignableFrom(obj.GetType())) { IList listObj = (IList)obj; int indx = Convert.ToInt32(StringUtility.ExtractFromDelimiters(refName, indexedRefStart, indexedRefEnd)); obj = listObj[indx]; } } refValue = obj; } } return(refValue); }