/// <summary> /// Resolves original parameterized template into a string by replacing parameters with values provided as /// a dictionary. /// </summary> /// <param name="parameters">Dictionary providing parameter values.</param> /// <returns>Resolved string if succeeded.</returns> /// <exception cref="InvalidOperationException">Thrown when required parameter value is not available. /// </exception> public string Bind(IReadOnlyDictionary <string, string> parameters) { StringBuilder builder = new StringBuilder(); if (_ignoreCase && parameters != null) { // convert to a case insensitive dictionary var caseInsensitive = new Dictionary <string, string>(parameters.Count, StringComparer.OrdinalIgnoreCase); foreach (var pair in parameters) { caseInsensitive.Add(pair.Key, pair.Value); } parameters = caseInsensitive; } foreach (BindingTemplateToken token in Tokens) { if (token.IsParameter) { string value; BindingParameterResolver resolver = null; if (parameters != null && parameters.TryGetValue(token.Value, out value)) { // parameter is resolved from binding data builder.Append(value); } else if (BindingParameterResolver.TryGetResolver(token.Value, out resolver)) { // parameter maps to one of the built-in system binding // parameters (e.g. rand-guid, datetime, etc.) value = resolver.Resolve(token.Value); builder.Append(value); } else { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "No value for named parameter '{0}'.", token.Value)); } } else { builder.Append(token.Value); } } return(builder.ToString()); }
public override string Evaluate(IReadOnlyDictionary <string, object> bindingData) { object current; if (bindingData.TryGetValue(this._expressionParts[0], out current)) { for (int i = 1; i < _expressionParts.Length; i++) { var propName = _expressionParts[i]; try { current = GetProperty(current, propName); } catch (Exception e) { throw new InvalidOperationException($"Error while accessing '{propName}': {e.Message}"); } } } else { // Not found in binding data. Is this a builtin? if (this._builtin != null) { current = _builtin.Resolve(this.ParameterName); } if (current == null) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "No value for named parameter '{0}'.", this.ParameterName)); } } var strValue = BindingDataPathHelper.ConvertParameterValueToString(current, _format); return(strValue); }