Пример #1
0
        private IEnumerable <AnalyzerTreeNode> FindReferencesWithinInType(TypeDef type, ITypeDefOrRef attrTypeRef)
        {
            bool searchRequired = (type.IsClass && usage.HasFlag(AttributeTargets.Class)) ||
                                  (type.IsEnum && usage.HasFlag(AttributeTargets.Enum)) ||
                                  (type.IsInterface && usage.HasFlag(AttributeTargets.Interface)) ||
                                  (Decompiler.DnlibExtensions.IsValueType(type) && usage.HasFlag(AttributeTargets.Struct));

            if (searchRequired)
            {
                if (type.HasCustomAttributes)
                {
                    foreach (var attribute in type.CustomAttributes)
                    {
                        if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                        {
                            var node = new AnalyzedTypeTreeNode(type);
                            node.Language = this.Language;
                            yield return(node);

                            break;
                        }
                    }
                }
            }

            if ((this.usage & AttributeTargets.GenericParameter) != 0 && type.HasGenericParameters)
            {
                foreach (var parameter in type.GenericParameters)
                {
                    if (parameter.HasCustomAttributes)
                    {
                        foreach (var attribute in parameter.CustomAttributes)
                        {
                            if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                            {
                                var node = new AnalyzedTypeTreeNode(type);
                                node.Language = this.Language;
                                yield return(node);

                                break;
                            }
                        }
                    }
                }
            }

            if ((this.usage & AttributeTargets.Field) != 0 && type.HasFields)
            {
                foreach (var field in type.Fields)
                {
                    if (field.HasCustomAttributes)
                    {
                        foreach (var attribute in field.CustomAttributes)
                        {
                            if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                            {
                                var node = new AnalyzedFieldTreeNode(field);
                                node.Language = this.Language;
                                yield return(node);

                                break;
                            }
                        }
                    }
                }
            }

            if (((usage & AttributeTargets.Property) != 0) && type.HasProperties)
            {
                foreach (var property in type.Properties)
                {
                    if (property.HasCustomAttributes)
                    {
                        foreach (var attribute in property.CustomAttributes)
                        {
                            if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                            {
                                var node = new AnalyzedPropertyTreeNode(property);
                                node.Language = this.Language;
                                yield return(node);

                                break;
                            }
                        }
                    }
                }
            }
            if (((usage & AttributeTargets.Event) != 0) && type.HasEvents)
            {
                foreach (var _event in type.Events)
                {
                    if (_event.HasCustomAttributes)
                    {
                        foreach (var attribute in _event.CustomAttributes)
                        {
                            if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                            {
                                var node = new AnalyzedEventTreeNode(_event);
                                node.Language = this.Language;
                                yield return(node);

                                break;
                            }
                        }
                    }
                }
            }

            if (type.HasMethods)
            {
                foreach (var method in type.Methods)
                {
                    bool found = false;
                    if ((usage & (AttributeTargets.Method | AttributeTargets.Constructor)) != 0)
                    {
                        if (method.HasCustomAttributes)
                        {
                            foreach (var attribute in method.CustomAttributes)
                            {
                                if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                                {
                                    found = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (!found &&
                        ((usage & AttributeTargets.ReturnValue) != 0) &&
                        method.Parameters.ReturnParameter.HasParamDef &&
                        method.Parameters.ReturnParameter.ParamDef.HasCustomAttributes)
                    {
                        foreach (var attribute in method.Parameters.ReturnParameter.ParamDef.CustomAttributes)
                        {
                            if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                            {
                                found = true;
                                break;
                            }
                        }
                    }

                    if (!found &&
                        ((usage & AttributeTargets.Parameter) != 0) &&
                        method.Parameters.Count > 0)
                    {
                        foreach (var parameter in method.Parameters.Where(param => param.HasParamDef))
                        {
                            if (parameter.IsHiddenThisParameter)
                            {
                                continue;
                            }
                            foreach (var attribute in parameter.ParamDef.CustomAttributes)
                            {
                                if (new SigComparer().Equals(attribute.AttributeType, attrTypeRef))
                                {
                                    found = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (found)
                    {
                        MethodDef codeLocation = this.Language.GetOriginalCodeLocation(method) as MethodDef;
                        if (codeLocation != null && !HasAlreadyBeenFound(codeLocation))
                        {
                            var node = new AnalyzedMethodTreeNode(codeLocation);
                            node.Language = this.Language;
                            yield return(node);
                        }
                    }
                }
            }
        }
Пример #2
0
        private IEnumerable<AnalyzerTreeNode> FindReferencesWithinInType(TypeDefinition type, TypeReference attrTypeRef)
        {
            bool searchRequired = (type.IsClass && usage.HasFlag(AttributeTargets.Class))
                || (type.IsEnum && usage.HasFlag(AttributeTargets.Enum))
                || (type.IsInterface && usage.HasFlag(AttributeTargets.Interface))
                || (type.IsValueType && usage.HasFlag(AttributeTargets.Struct));
            if (searchRequired) {
                if (type.HasCustomAttributes) {
                    foreach (var attribute in type.CustomAttributes) {
                        if (attribute.AttributeType == attrTypeRef) {
                            var node = new AnalyzedTypeTreeNode(type);
                            node.Language = this.Language;
                            yield return node;
                            break;
                        }
                    }
                }
            }

            if ((this.usage & AttributeTargets.GenericParameter) != 0 && type.HasGenericParameters) {
                foreach (var parameter in type.GenericParameters) {
                    if (parameter.HasCustomAttributes) {
                        foreach (var attribute in parameter.CustomAttributes) {
                            if (attribute.AttributeType == attrTypeRef) {
                                var node = new AnalyzedTypeTreeNode(type);
                                node.Language = this.Language;
                                yield return node;
                                break;
                            }
                        }
                    }
                }
            }

            if ((this.usage & AttributeTargets.Field) != 0 && type.HasFields) {
                foreach (var field in type.Fields) {
                    if (field.HasCustomAttributes) {
                        foreach (var attribute in field.CustomAttributes) {
                            if (attribute.AttributeType == attrTypeRef) {
                                var node = new AnalyzedFieldTreeNode(field);
                                node.Language = this.Language;
                                yield return node;
                                break;
                            }
                        }
                    }
                }
            }

            if (((usage & AttributeTargets.Property) != 0) && type.HasProperties) {
                foreach (var property in type.Properties) {
                    if (property.HasCustomAttributes) {
                        foreach (var attribute in property.CustomAttributes) {
                            if (attribute.AttributeType == attrTypeRef) {
                                var node = new AnalyzedPropertyTreeNode(property);
                                node.Language = this.Language;
                                yield return node;
                                break;
                            }
                        }
                    }
                }
            }
            if (((usage & AttributeTargets.Event) != 0) && type.HasEvents) {
                foreach (var _event in type.Events) {
                    if (_event.HasCustomAttributes) {
                        foreach (var attribute in _event.CustomAttributes) {
                            if (attribute.AttributeType == attrTypeRef) {
                                var node = new AnalyzedEventTreeNode(_event);
                                node.Language = this.Language;
                                yield return node;
                                break;
                            }
                        }
                    }
                }
            }

            if (type.HasMethods) {
                foreach (var method in type.Methods) {
                    bool found = false;
                    if ((usage & (AttributeTargets.Method | AttributeTargets.Constructor)) != 0) {
                        if (method.HasCustomAttributes) {
                            foreach (var attribute in method.CustomAttributes) {
                                if (attribute.AttributeType == attrTypeRef) {
                                    found = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (!found &&
                        ((usage & AttributeTargets.ReturnValue) != 0) &&
                        method.MethodReturnType.HasCustomAttributes) {
                        foreach (var attribute in method.MethodReturnType.CustomAttributes) {
                            if (attribute.AttributeType == attrTypeRef) {
                                found = true;
                                break;
                            }
                        }
                    }

                    if (!found &&
                        ((usage & AttributeTargets.Parameter) != 0) &&
                        method.HasParameters) {
                        foreach (var parameter in method.Parameters) {
                            foreach (var attribute in parameter.CustomAttributes) {
                                if (attribute.AttributeType == attrTypeRef) {
                                    found = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (found) {
                        MethodDefinition codeLocation = this.Language.GetOriginalCodeLocation(method) as MethodDefinition;
                        if (codeLocation != null && !HasAlreadyBeenFound(codeLocation)) {
                            var node = new AnalyzedMethodTreeNode(codeLocation);
                            node.Language = this.Language;
                            yield return node;
                        }
                    }
                }
            }
        }