Esempio n. 1
0
        /// <summary>
        /// Converts this abstract representation of a C# class into a string representation (i.e. code).
        /// </summary>
        /// <returns>The string representation of this class.</returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            // Documentation comment
            string docComment = this.DocumentationComment?.ToString();

            if (!string.IsNullOrWhiteSpace(docComment))
            {
                sb.AppendLine(docComment);
            }

            // Attributes
            foreach (CSharpAttribute attribute in this.Attributes)
            {
                sb.AppendLine(attribute.ToString());
            }

            // Add the first line of the class definition (access modifiers, class name, inheritance)
            IEnumerable <string> parents = this.BaseType == null
                ? this.Interfaces
                : this.BaseType.SingleObjectAsEnumerable().Concat(this.Interfaces);

            sb.Append(AccessModifier.ToCSharpString());
            if (this.IsStatic)
            {
                sb.Append(" static");
            }
            sb.Append($" class {this.Name}");
            if (parents.Any())
            {
                sb.Append(" : ");
                sb.Append(string.Join(", ", parents));
            }
            sb.AppendLine();

            // Start body
            sb.AppendLine("{");

            // Properties
            bool isFirst = true;

            foreach (CSharpProperty property in this.Properties)
            {
                // Add a new line except for the first property
                if (isFirst)
                {
                    isFirst = false;
                }
                else
                {
                    sb.AppendLine();
                }

                sb.AppendLine(property.ToString().Indent());
            }

            if (this.Properties.Any() && this.Methods.Any())
            {
                sb.AppendLine();
            }

            // Constructors and methods
            isFirst = true;
            foreach (CSharpMethod method in this.Constructors.Concat(this.Methods))
            {
                // Add a new line except for the first property
                if (isFirst)
                {
                    isFirst = false;
                }
                else
                {
                    sb.AppendLine();
                }

                sb.AppendLine(method.ToString().Indent());
            }

            // End body
            sb.Append("}");

            // Compile and return result
            string result = sb.ToString();

            return(result);
        }