예제 #1
0
        private void FillAttributes(Type type, MemberInfo member, Type attributeType, Func <object, object> reflectionMapper, List <object> compiledAttributes, Dictionary <Type, object> seenAttributes)
        {
            Type       currentType          = type;
            MemberInfo memberInfo           = member;
            bool       firstIteration       = true;
            bool       flag                 = member is Type;
            bool       includeClrAttributes = flag || memberInfo.DeclaringType == currentType;
            Type       runtimeAttributeType = attributeType;

            if (reflectionMapper != null)
            {
                runtimeAttributeType = (Type)reflectionMapper((object)attributeType);
            }
            if (runtimeAttributeType == null && attributeType != null)
            {
                return;
            }
            while (currentType != null && memberInfo != null)
            {
                this.FillAttributesHelper(attributeType, reflectionMapper, compiledAttributes, seenAttributes, currentType, memberInfo, firstIteration, includeClrAttributes, runtimeAttributeType);
                firstIteration = false;
                if (flag && currentType.IsGenericType)
                {
                    Type genericTypeDefinition = currentType.GetGenericTypeDefinition();
                    this.FillAttributesHelper(attributeType, reflectionMapper, compiledAttributes, seenAttributes, genericTypeDefinition, (MemberInfo)genericTypeDefinition, firstIteration, includeClrAttributes, runtimeAttributeType);
                }
                if (flag || memberInfo.DeclaringType == currentType)
                {
                    memberInfo = AttributeDataCache.GetBaseMemberInfo(memberInfo);
                }
                currentType          = currentType.BaseType;
                includeClrAttributes = flag || memberInfo == null || memberInfo.DeclaringType == currentType;
            }
        }
예제 #2
0
        public IEnumerable <object> GetAttributes(Assembly assembly, Type attributeType, Func <object, object> reflectionMapper)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }
            List <object>             list;
            Dictionary <Type, object> dictionary;

            lock (this._syncLock)
            {
                list       = this._compiledAttributes;
                dictionary = this._seenAttributes;
                this._compiledAttributes = (List <object>)null;
                this._seenAttributes     = (Dictionary <Type, object>)null;
            }
            if (list == null)
            {
                list = new List <object>();
            }
            else
            {
                list.Clear();
            }
            if (dictionary == null)
            {
                dictionary = new Dictionary <Type, object>();
            }
            else
            {
                dictionary.Clear();
            }
            foreach (object obj in this.MergeAttributesIterator(assembly, attributeType, true, reflectionMapper))
            {
                AttributeData attributeData = AttributeDataCache.GetAttributeData(obj.GetType());
                if (!dictionary.ContainsKey(attributeData.AttributeType) || attributeData.AllowsMultiple)
                {
                    list.Add(obj);
                    dictionary[attributeData.AttributeType] = obj;
                }
            }
            object[] objArray = list.ToArray();
            lock (this._syncLock)
            {
                this._compiledAttributes = list;
                this._seenAttributes     = dictionary;
            }
            return((IEnumerable <object>)objArray);
        }
예제 #3
0
        private void FillAttributesHelper(Type attributeType, Func <object, object> reflectionMapper, List <object> compiledAttributes, Dictionary <Type, object> seenAttributes, Type currentType, MemberInfo currentMember, bool firstIteration, bool includeClrAttributes, Type runtimeAttributeType)
        {
            Type runtimeType = reflectionMapper == null ? currentType : (Type)reflectionMapper((object)currentType);

            if (runtimeType == null && currentType != null)
            {
                return;
            }
            foreach (object obj in this.MergeAttributesIterator(currentType, currentMember, attributeType, runtimeType, runtimeAttributeType, includeClrAttributes))
            {
                AttributeData attributeData = AttributeDataCache.GetAttributeData(obj.GetType());
                if ((!seenAttributes.ContainsKey(attributeData.AttributeType) || attributeData.AllowsMultiple) && (firstIteration || attributeData.IsInheritable))
                {
                    compiledAttributes.Add(obj);
                    seenAttributes[attributeData.AttributeType] = obj;
                }
            }
        }
예제 #4
0
        private IEnumerable <object> MergeAttributesIterator(Assembly assembly, Type attributeType, bool includeClrAttributes, Func <object, object> reflectionMapper)
        {
            AttributeTable[] tables               = this.AttributeTableArray;
            Assembly         runtimeAssembly      = assembly;
            Type             runtimeAttributeType = attributeType;

            if (reflectionMapper != null)
            {
                runtimeAssembly = (Assembly)reflectionMapper((object)assembly);
                if (runtimeAttributeType != null)
                {
                    runtimeAttributeType = (Type)reflectionMapper((object)attributeType);
                }
            }
            if ((runtimeAssembly != null || assembly == null) && (runtimeAttributeType != null || attributeType == null))
            {
                foreach (object o in AttributeDataCache.GetAttributeTableAttributes(runtimeAssembly, (Type)null, (string)null, tables))
                {
                    if (runtimeAttributeType == null || runtimeAttributeType.IsInstanceOfType(o))
                    {
                        yield return(o);
                    }
                }
                if (includeClrAttributes)
                {
                    IEnumerable <object> enumerator = AttributeDataCache.GetClrAttributes(assembly, (MemberInfo)null, attributeType);
                    if (enumerator != null)
                    {
                        foreach (object obj in enumerator)
                        {
                            yield return(obj);
                        }
                    }
                }
            }
        }
예제 #5
0
        private IEnumerable <object> MergeAttributesIterator(Type type, MemberInfo member, Type attributeType, Type runtimeType, Type runtimeAttributeType, bool includeClrAttributes)
        {
            string memberName = member is Type ? (string)null : member.Name;

            AttributeTable[] tables = this.AttributeTableArray;
            foreach (object o in AttributeDataCache.GetAttributeTableAttributes((Assembly)null, runtimeType, memberName, tables))
            {
                if (runtimeAttributeType == null || runtimeAttributeType.IsInstanceOfType(o))
                {
                    yield return(o);
                }
            }
            if (includeClrAttributes)
            {
                IEnumerable <object> enumerator = AttributeDataCache.GetClrAttributes((Assembly)null, member, attributeType);
                if (enumerator != null)
                {
                    foreach (object obj in enumerator)
                    {
                        yield return(obj);
                    }
                }
            }
        }