コード例 #1
0
        /// <summary>
        /// Sanitizes a C# string if required, otherwise returns the original string.
        /// </summary>
        /// <param name="identifier">The identifier to sanitize.</param>
        /// <returns>The sanitized C# keyword which is safe to use as an identifier.</returns>
        public static string SanitizeIdentifier(string identifier)
        {
            if (identifier == null)
            {
                throw new ArgumentNullException(nameof(identifier));
            }

            if (CSharpNamingUtils.IsValidIdentifier(identifier))
            {
                return(identifier);
            }
            else
            {
                // Add an "@" to escape keywords
                string result = $"@{identifier}";

                // Make sure that it is now valid - if it isn't, it was never a C# keyword to begin with.
                // It was just an invalid identifier, probably with special characters or numbers.
                if (!CSharpNamingUtils.IsValidIdentifier(result))
                {
                    throw new ArgumentException($"Invalid characters found in identifier '{identifier}'", nameof(identifier));
                }

                return(result);
            }
        }
コード例 #2
0
        /// <summary>
        /// Converts this abstract representation of a C# property into a string representation (i.e. code).
        /// </summary>
        /// <returns>The string representation of this C# property.</returns>
        public override string ToString()
        {
            // Create a string builder
            StringBuilder sb = new StringBuilder();

            // Add the documentation comment
            string docComment = this.DocumentationComment?.ToString();

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

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

            // Add the property definition itself
            sb.Append(this.AccessModifier.ToCSharpString());
            if (this.IsStatic)
            {
                sb.Append(" static");
            }
            if (this.IsOverride)
            {
                sb.Append(" override");
            }
            sb.Append($" {this.TypeName} {CSharpNamingUtils.SanitizeIdentifier(this.Name)}");
            sb.Append(" {");
            if (this.HasGetter)
            {
                sb.Append(" get;");
            }
            if (this.HasSetter)
            {
                sb.Append(" set;");
            }
            sb.Append(" }");

            // Add the default value
            if (!string.IsNullOrWhiteSpace(this.DefaultValue))
            {
                sb.Append($" = {this.DefaultValue};");
            }

            // Compile the string
            string result = sb.ToString();

            return(result);
        }
コード例 #3
0
 /// <summary>
 /// Converts this abstract representation of a C# parameter into a string representation (i.e. code).
 /// </summary>
 /// <returns>The string representation of this C# parameter.</returns>
 public override string ToString()
 {
     return($"{this.TypeName} {CSharpNamingUtils.SanitizeIdentifier(this.Name)}");
 }