Exemple #1
0
        /// <summary>
        /// Convert this object to a string in the specified format.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="format">The format to be used to construct the text string.
        /// This should take the form of a string interspersed with property paths surrounded by
        /// open and close tag characters, the values of which will be inserted into the text.
        /// For example "The value of the subproperty is: {PropertyName.SubPropertyName}"</param>
        /// <param name="openTag">The opening tag for specifying property paths.  Default is '{'.</param>
        /// <param name="closeTag">The closing tag for specifying property paths.  Default is '}'.</param>
        /// <param name="context">The (optional) string conversion context object.  If supplies this allows
        /// the '*' symbol to be used within property paths in order to access properties and
        /// functions supplied on the context object.</param>
        /// <returns></returns>
        public static string ToString(this object obj, string format, char openTag = '{', char closeTag = '}',
                                      string ifTag = TextFormat.IF, string thenTag = TextFormat.THEN, IStringConversionContext context = null)
        {
            StringBuilder resultBuilder = new StringBuilder();

            StringBuilder pathBuilder = new StringBuilder();

            context.SubComponentIndex = 0;

            if (context.HasSubComponentsToWrite(obj))
            {
                int oldIndex = context.SubComponentIndex;
                //Sub-items:
                while (context.HasSubComponentsToWrite(obj))
                {
                    CreateFormattedString(obj, format, openTag, closeTag,
                                          ifTag, thenTag, context, resultBuilder, pathBuilder);
                    resultBuilder.AppendLine();
                    context.SubComponentIndex++;
                }
                context.SubComponentIndex = oldIndex;
            }
            else
            {
                CreateFormattedString(obj, format, openTag, closeTag,
                                      ifTag, thenTag, context, resultBuilder, pathBuilder);
            }

            return(resultBuilder.ToString());
        }