Esempio n. 1
0
        /// <summary>
        /// Replaces property names in the source string with the actual
        /// values of the properties.
        /// </summary>
        /// <param name="delimiterMode">
        /// Specifies which delimiter or delimiters to use for parsing
        /// property names.
        /// </param>
        /// <param name="obj">Object that contains the property values.</param>
        /// <param name="source">
        /// String containing property references in the format
        /// specified by the <see cref="DelimiterMode"/>
        /// </param>
        /// <returns>
        /// The source string with all property references replaced with
        /// actual property values.
        /// </returns>
        public static string ResolvePropertyValues(DelimiterMode delimiterMode, object obj, string source)
        {
            if (delimiterMode == DelimiterMode.CurlyBraces)
            {
                return(ResolvePropertyValues("{", "}", obj, source));
            }
            else if (delimiterMode == DelimiterMode.DollarParen)
            {
                return(ResolvePropertyValues("$(", ")", obj, source));
            }
            else if (delimiterMode == DelimiterMode.Any)
            {
                var res = ResolvePropertyValues("{", "}", obj, source);
                res = ResolvePropertyValues("$(", ")", obj, res);
                return(res);
            }

            var msg = string.Format("Delimiter mode {0} is not currently supported", Enum.GetName(typeof(DelimiterMode), delimiterMode));

            throw new ArgumentException(msg, "delimiterMode");
        }
Esempio n. 2
0
        /// <summary>
        /// Attempts to resolve the source string as a single
        /// variable name. This only works if the entire source
        /// string is a single variable name.
        /// </summary>
        /// <param name="delimiterMode">
        /// Specifies which delimiter or delimiters to use for parsing
        /// property names.
        /// </param>
        /// <param name="obj">
        /// Object containing the property value.
        /// </param>
        /// <param name="source">
        /// String containing property name as a variable.
        /// </param>
        /// <param name="propValue">Value of the property found</param>
        /// <returns>
        /// Returns true if the source string consists of a single variable name
        /// and the variable name is resolved.
        /// </returns>
        public static bool TryResolveSinglePropertyValue(DelimiterMode delimiterMode, object obj, string source, out object propValue)
        {
            if (delimiterMode == DelimiterMode.CurlyBraces)
            {
                return(TryResolveSinglePropertyValue("{", "}", obj, source, out propValue));
            }
            else if (delimiterMode == DelimiterMode.DollarParen)
            {
                return(TryResolveSinglePropertyValue("$(", ")", obj, source, out propValue));
            }
            else if (delimiterMode == DelimiterMode.Any)
            {
                if (TryResolveSinglePropertyValue("$(", ")", obj, source, out propValue))
                {
                    return(true);
                }
                return(TryResolveSinglePropertyValue("{", "}", obj, source, out propValue));
            }

            var msg = string.Format("Delimiter mode {0} is not currently supported", Enum.GetName(typeof(DelimiterMode), delimiterMode));

            throw new ArgumentException(msg, "delimiterMode");
        }