示例#1
0
        public string GetName(IEmitter emitter)
        {
            string fieldName = this.Name;

            if (this.VarInitializer != null)
            {
                var rr = emitter.Resolver.ResolveNode(this.VarInitializer, emitter) as MemberResolveResult;
                fieldName = OverloadsCollection.Create(emitter, rr.Member).GetOverloadName();
            }
            else if (this.Entity is PropertyDeclaration)
            {
                fieldName = OverloadsCollection.Create(emitter, (PropertyDeclaration)this.Entity).GetOverloadName();
            }
            else
            {
                bool done = false;
                if (this.Entity != null)
                {
                    var rr = emitter.Resolver.ResolveNode(this.Entity, emitter) as MemberResolveResult;

                    if (rr != null)
                    {
                        fieldName = OverloadsCollection.Create(emitter, rr.Member).GetOverloadName();
                        done      = true;
                    }
                }

                if (!done)
                {
                    fieldName = emitter.AssemblyInfo.PreserveMemberCase ? fieldName : TypeConfigItem.ToLowerCamelCase(fieldName);

                    if (Helpers.IsReservedWord(fieldName))
                    {
                        fieldName = Helpers.ChangeReservedWord(fieldName);
                    }
                }
            }
            return(fieldName);
        }
示例#2
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);
        }