ICSharpCode.NRefactory.CSharp.Resolver.FindReferences.GetInterestingFiles C# (CSharp) Méthode

GetInterestingFiles() public méthode

Gets the file names that possibly contain references to the element being searched for.
public GetInterestingFiles ( IFindReferenceSearchScope searchScope, ICompilation compilation ) : IEnumerable
searchScope IFindReferenceSearchScope
compilation ICompilation
Résultat IEnumerable
		public IEnumerable<CSharpUnresolvedFile> GetInterestingFiles(IFindReferenceSearchScope searchScope, ICompilation compilation)
		{
			if (searchScope == null)
				throw new ArgumentNullException("searchScope");
			if (compilation == null)
				throw new ArgumentNullException("compilation");
			var pc = compilation.MainAssembly.UnresolvedAssembly as IProjectContent;
			if (pc == null)
				throw new ArgumentException("Main assembly is not a project content");
			if (searchScope.TopLevelTypeDefinition != null) {
				ITypeDefinition topLevelTypeDef = compilation.Import(searchScope.TopLevelTypeDefinition);
				if (topLevelTypeDef == null) {
					// This compilation cannot have references to the target entity.
					return EmptyList<CSharpUnresolvedFile>.Instance;
				}
				switch (searchScope.Accessibility) {
					case Accessibility.None:
					case Accessibility.Private:
						if (topLevelTypeDef.ParentAssembly == compilation.MainAssembly)
							return topLevelTypeDef.Parts.Select(p => p.UnresolvedFile).OfType<CSharpUnresolvedFile>().Distinct();
						else
							return EmptyList<CSharpUnresolvedFile>.Instance;
					case Accessibility.Protected:
						return GetInterestingFilesProtected(topLevelTypeDef);
					case Accessibility.Internal:
						if (topLevelTypeDef.ParentAssembly.InternalsVisibleTo(compilation.MainAssembly))
							return pc.Files.OfType<CSharpUnresolvedFile>();
						else
							return EmptyList<CSharpUnresolvedFile>.Instance;
					case Accessibility.ProtectedAndInternal:
						if (topLevelTypeDef.ParentAssembly.InternalsVisibleTo(compilation.MainAssembly))
							return GetInterestingFilesProtected(topLevelTypeDef);
						else
							return EmptyList<CSharpUnresolvedFile>.Instance;
					case Accessibility.ProtectedOrInternal:
						if (topLevelTypeDef.ParentAssembly.InternalsVisibleTo(compilation.MainAssembly))
							return pc.Files.OfType<CSharpUnresolvedFile>();
						else
							return GetInterestingFilesProtected(topLevelTypeDef);
					default:
						return pc.Files.OfType<CSharpUnresolvedFile>();
				}
			} else {
				if (searchScope.FileName == null)
					return pc.Files.OfType<CSharpUnresolvedFile>();
				else
					return pc.Files.OfType<CSharpUnresolvedFile>().Where(f => f.FileName == searchScope.FileName);
			}
		}
		

Usage Example

        void TestFindReferences(IEntity entity)
        {
            if (IgnoreEntity(entity))
                return;
            FindReferences fr = new FindReferences();
            fr.FindTypeReferencesEvenIfAliased = true;

            Stopwatch w = new Stopwatch();
            var searchScopes = fr.GetSearchScopes(entity);
            foreach (var project in solution.Projects) {
                w.Restart();
                HashSet<AstNode> foundReferences = new HashSet<AstNode>();
                var interestingFiles = new HashSet<CSharpFile>();
                foreach (var searchScope in searchScopes) {
                    foreach (var unresolvedFile in fr.GetInterestingFiles(searchScope, project.Compilation)) {
                        var file = project.Files.Single(f => f.FileName == unresolvedFile.FileName);
                        Debug.Assert(file.UnresolvedTypeSystemForFile == unresolvedFile);

                        // Skip file if it doesn't contain the search term
                        if (searchScope.SearchTerm != null && file.OriginalText.IndexOf(searchScope.SearchTerm, StringComparison.Ordinal) < 0)
                            continue;

                        interestingFiles.Add(file);
                    }
                }
                foreach (var file in interestingFiles) {
                    fr.FindReferencesInFile(searchScopes, file.UnresolvedTypeSystemForFile, file.SyntaxTree, project.Compilation,
                                            delegate(AstNode node, ResolveResult result) {
                                                foundReferences.Add(node);
                                            }, CancellationToken.None);
                }
                w.Stop();
                if (timings.ContainsKey(entity.EntityType)) {
                    timings[entity.EntityType] += w.Elapsed;
                } else {
                    timings[entity.EntityType] = w.Elapsed;
                }

                IEntity importedEntity = project.Compilation.Import(entity);

                HashSet<AstNode> expectedReferences;
                if (importedEntity == null || !referenceDict.TryGetValue(importedEntity, out expectedReferences)) {
                    if (foundReferences.Any()) {
                        // There aren't any expected references stored, but we found some references anyways:
                        Console.WriteLine();
                        Console.WriteLine("Entity not in reference dictionary: " + entity);
                    }
                    return;
                }
                if (foundReferences.Except(expectedReferences).Any()) {
                    Console.WriteLine();
                    Console.WriteLine("Reference mismatch for " + entity + ":");
                    var n = foundReferences.Except(expectedReferences).First();
                    Console.WriteLine("Found unexpected reference " + n + " (" + n.GetRegion() + ")");
                }
                if (expectedReferences.Except(foundReferences).Any()) {
                    Console.WriteLine();
                    Console.WriteLine("Reference mismatch for " + entity + ":");
                    var n = expectedReferences.Except(foundReferences).First();
                    Console.WriteLine("Did not find expected reference " + n + " (" + n.GetRegion() + ")");
                }
            }

            if (entityCount.ContainsKey(entity.EntityType)) {
                entityCount[entity.EntityType]++;
            } else {
                entityCount[entity.EntityType] = 1;
            }
        }
All Usage Examples Of ICSharpCode.NRefactory.CSharp.Resolver.FindReferences::GetInterestingFiles