Ejemplo n.º 1
0
        private static NameRule[] GetClassRules(NameSemantic semantic, ITypeDefinition typeDef)
        {
            if (semantic.Emitter.ClassNameRuleCache.ContainsKey(typeDef))
            {
                return(semantic.Emitter.ClassNameRuleCache[typeDef]);
            }

            var             td    = typeDef;
            List <NameRule> rules = new List <NameRule>();

            while (td != null)
            {
                IAttribute[] classAttrs = td.GetAttributes(new FullTypeName(NameConvertor.ConventionAttrName)).ToArray();
                for (int i = 0; i < classAttrs.Length; i++)
                {
                    rules.Add(NameConvertor.ToRule(classAttrs[i], NameRuleLevel.Class));
                }

                td = td.DeclaringTypeDefinition;
            }

            var classRules = rules.ToArray();

            semantic.Emitter.ClassNameRuleCache.Add(typeDef, classRules);
            return(classRules);
        }
Ejemplo n.º 2
0
        private static List <NameRule> GetAttributeRules(NameSemantic semantic)
        {
            NameRule memberRule = null;

            NameRule[] classRules     = null;
            NameRule[] assemblyRules  = null;
            NameRule[] interfaceRules = null;

            if (semantic.Entity is IMember)
            {
                var attr = Helpers.GetInheritedAttribute(semantic.Entity, NameConvertor.ConventionAttrName);

                if (attr != null)
                {
                    memberRule = NameConvertor.ToRule(attr, NameRuleLevel.Member);
                }

                var typeDef = semantic.Entity.DeclaringTypeDefinition;

                if (typeDef != null)
                {
                    classRules = NameConvertor.GetClassRules(semantic, typeDef);
                }

                interfaceRules = NameConvertor.GetVirtualMemberRules(semantic);
            }
            else if (semantic.Entity is ITypeDefinition)
            {
                classRules = NameConvertor.GetClassRules(semantic, (ITypeDefinition)semantic.Entity);
            }

            var assembly = semantic.Entity.ParentAssembly;

            if (semantic.Emitter.AssemblyNameRuleCache.ContainsKey(assembly))
            {
                assemblyRules = semantic.Emitter.AssemblyNameRuleCache[assembly];
            }
            else
            {
                IAttribute[] assemblyAttrs = assembly.AssemblyAttributes.Where(a => a.AttributeType.FullName == NameConvertor.ConventionAttrName).ToArray();
                assemblyRules = new NameRule[assemblyAttrs.Length];
                for (int i = 0; i < assemblyAttrs.Length; i++)
                {
                    assemblyRules[i] = NameConvertor.ToRule(assemblyAttrs[i], NameRuleLevel.Assembly);
                }

                Array.Sort(assemblyRules, (item1, item2) => - item1.Priority.CompareTo(item2.Priority));

                semantic.Emitter.AssemblyNameRuleCache.Add(assembly, assemblyRules);
            }

            var rules = new List <NameRule>();

            if (memberRule != null)
            {
                rules.Add(memberRule);
            }

            if (classRules != null && classRules.Length > 0)
            {
                rules.AddRange(classRules);
            }

            if (interfaceRules != null && interfaceRules.Length > 0)
            {
                rules.AddRange(interfaceRules);
            }

            if (assemblyRules != null && assemblyRules.Length > 0)
            {
                rules.AddRange(assemblyRules);
            }

            return(rules);
        }