Пример #1
0
        internal static string AlterFirstCharCase(string memberName, TargetCase firstCharCase)
        {
            if (string.IsNullOrEmpty(memberName))
            {
                return(memberName);
            }

            char c = memberName[0];
            char newChar;

            switch (firstCharCase)
            {
            case TargetCase.ToUpper:
                newChar = Char.ToUpper(c);
                break;

            case TargetCase.ToLower:
                newChar = Char.ToLower(c);
                break;

            case TargetCase.DontChange:
                return(memberName);

            default:
                throw new ArgumentOutOfRangeException("firstCharCase");
            }

            return(newChar + memberName.Substring(1));
        }
Пример #2
0
        /// <summary>
        /// Creates a safe member name which can be used within code environments.
        /// Also checks for disallowed C# keywords words.
        /// </summary>
        public static string GetSafeMemberName(IEnumerable <string> unsafeWords,
                                               TargetCase firstCharCase,
                                               string baseName,
                                               params string[] alternativeNames)
        {
            unsafeWords.ThrowIfNull("unsafeWords");
            baseName.ThrowIfNullOrEmpty("baseName");
            alternativeNames.ThrowIfNull("alternativeNames");

            // Watch for both C# language keywords and member names used in this scope.
            IEnumerable <string> illegalWords = UnsafeWords.Concat(unsafeWords);

            // Check whether the base name, or one of the proposed alternative names can be used.
            foreach (string proposedName in GenerateAlternativeNamesFor(baseName, alternativeNames))
            {
                // Validate/try making valid name out of the name
                string validName      = MakeValidMemberName(proposedName);
                string casedValidName = AlterFirstCharCase(validName, firstCharCase);

                if (IsNameValidInContext(casedValidName, illegalWords))
                {
                    // We have a valid name - return the result.
                    return(casedValidName);
                }
            }

            // We are out of meaningful naming options: Throw an exception.
            throw new ArgumentException(
                      string.Format(
                          "Unable to generate safe member name: Out of meaningful names for member '{0}'", baseName));
        }