Example #1
0
        public bool HasAttribute(MetadataReader reader, CustomAttributeHandleCollection attrHandles, string attributeFullName)
        {
            if (!_attributeConstructors.TryGetValue(attributeFullName, out var constructorSet))
            {
                constructorSet = new HashSet <EntityHandle>();
                _attributeConstructors[attributeFullName] = constructorSet;
            }

            var attrs = attrHandles.Select(reader.GetCustomAttribute).ToList();

            if (attrs.Any(attr => constructorSet.Contains(attr.Constructor)))
            {
                return(true);
            }

            var compilerGeneratedAttr = attrs
                                        .Where(attr => reader.GetFullname(reader.GetCustomAttrClass(attr)) == attributeFullName)
                                        .Select(attr => (CustomAttribute?)attr)
                                        .FirstOrDefault();

            if (compilerGeneratedAttr == null)
            {
                return(false);
            }

            constructorSet.Add(compilerGeneratedAttr.Value.Constructor);
            return(true);
        }