예제 #1
0
        public static CompilerRule Get(IEmitter emitter, IEntity entity)
        {
            CompilerRule memberRule = null;

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

            if (entity is IMember)
            {
                var attr = Helpers.GetInheritedAttribute(entity, attributeName);

                if (attr != null)
                {
                    memberRule = Rules.ToRule(attr, CompilerRuleLevel.Member);
                }

                var typeDef = entity.DeclaringTypeDefinition;

                if (typeDef != null)
                {
                    classRules = Rules.GetClassRules(emitter, typeDef);
                }

                interfaceRules = Rules.GetVirtualMemberRules(emitter, entity);
            }
            else if (entity is ITypeDefinition)
            {
                classRules = Rules.GetClassRules(emitter, (ITypeDefinition)entity);
            }

            var assembly = entity.ParentAssembly;

            if (emitter != null && emitter.AssemblyCompilerRuleCache.ContainsKey(assembly))
            {
                assemblyRules = emitter.AssemblyCompilerRuleCache[assembly];
            }
            else
            {
                IAttribute[] assemblyAttrs = assembly.AssemblyAttributes.Where(a => a.AttributeType.FullName == Rules.attributeName).ToArray();
                assemblyRules = new CompilerRule[assemblyAttrs.Length];
                for (int i = 0; i < assemblyAttrs.Length; i++)
                {
                    assemblyRules[i] = Rules.ToRule(assemblyAttrs[i], CompilerRuleLevel.Assembly);
                }

                if (emitter != null)
                {
                    emitter.AssemblyCompilerRuleCache.Add(assembly, assemblyRules);
                }
            }

            var rules = new List <CompilerRule>();

            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 (emitter != null)
            {
                rules.Add(emitter.AssemblyInfo.Rules);
            }

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

            //Must be last, as MergeRules works backwards
            if (!assembly.AssemblyName.StartsWith("H5"))
            {
                rules.Add(CompilerRule.DefaultIfNotH5());
            }

            return(MergeRules(rules));
        }