示例#1
0
        private static string ApplyRule(NameSemantic semantic, NameRule rule, string customName)
        {
            semantic.AppliedRule = rule;
            var name = semantic.DefaultName;

            if (rule != null)
            {
                if (!string.IsNullOrWhiteSpace(rule.CustomName))
                {
                    customName = rule.CustomName;
                }

                switch (rule.Notation)
                {
                case Notation.None:
                    break;

                case Notation.UpperCase:
                    name = name.ToUpperInvariant();
                    break;

                case Notation.LowerCase:
                    name = name.ToLowerInvariant();
                    break;

                case Notation.CamelCase:
                    var rejectRule = rule.Level != NameRuleLevel.Member && semantic.Entity is IMember &&
                                     !semantic.IsCustomName &&
                                     semantic.Entity.Name.Length > 1 &&
                                     semantic.Entity.Name.ToUpperInvariant() == semantic.Entity.Name;
                    if (rejectRule)
                    {
                        int upperCount = 0;
                        for (int i = 0; i < semantic.Entity.Name.Length; i++)
                        {
                            if (char.IsUpper(semantic.Entity.Name[i]))
                            {
                                upperCount++;
                            }
                            if (char.IsLower(semantic.Entity.Name[i]))
                            {
                                rejectRule = false;
                                break;
                            }
                        }

                        if (upperCount <= 1)
                        {
                            rejectRule = false;
                        }
                    }

                    if (!rejectRule)
                    {
                        name = name.ToLowerCamelCase();
                    }

                    break;

                case Notation.PascalCase:
                    name = name.ToCamelCase();
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(rule.Notation), rule.Notation, null);
                }
            }

            if (!string.IsNullOrWhiteSpace(customName))
            {
                name = Helpers.ConvertNameTokens(customName, name);
            }

            if (semantic.Entity is IMember)
            {
                bool isIgnore = semantic.Entity.DeclaringTypeDefinition != null && semantic.Emitter.Validator.IsExternalType(semantic.Entity.DeclaringTypeDefinition);
                if (!isIgnore && semantic.Entity.IsStatic && Helpers.IsReservedStaticName(name, false))
                {
                    name = Helpers.ChangeReservedWord(name);
                }
            }

            return(name);
        }