private static void ThrowDataMissException(string name, bool skipRecursiveLookup) { var ex = new StubbleDataMissException($"'{name}' is undefined."); ex.Data["Name"] = name; ex.Data["SkipRecursiveLookup"] = skipRecursiveLookup; throw ex; }
/// <summary> /// Looks up a value by name from the context /// </summary> /// <param name="name">The name of the value to lookup</param> /// <exception cref="StubbleDataMissException">If ThrowOnDataMiss set then thrown on value not found</exception> /// <returns>The value if found or null if not</returns> public Expression Lookup(string name) { var instance = SourceData; Expression value = null; if (cache.TryGetValue(name, out var outValue)) { value = outValue; } else { var context = this; var lookupHit = false; while (context != null) { var type = context.View; if (name.IndexOf('.') > 0) { var names = name.Split('.'); for (var i = 0; i < names.Length; i++) { var tempValue = GetValueFromRegistry(type, instance, names[i]); if (tempValue.Expression != null && tempValue.Type != null) { if (i == names.Length - 1) { lookupHit = true; } type = tempValue.Type; instance = tempValue.Expression; value = tempValue.Expression; } else if (i > 0) { return(null); } else { break; } } } else if (type != null) { var tempValue = GetValueFromRegistry(type, instance, name); if (tempValue.Expression != null) { lookupHit = true; value = tempValue.Expression; } } if (lookupHit || CompilationSettings.SkipRecursiveLookup) { break; } context = context.ParentContext; instance = context?.SourceData; } value = TryEnumerationConversionIfRequired(value); cache[name] = value; } if (!CompilationSettings.ThrowOnDataMiss || value != null) { return(value); } var ex = new StubbleDataMissException($"'{name}' is undefined."); ex.Data["Name"] = name; ex.Data["SkipRecursiveLookup"] = CompilationSettings.SkipRecursiveLookup; throw ex; }