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

FindLocalReferences() public méthode

Finds all references of a given variable.
public FindLocalReferences ( IVariable variable, CSharpUnresolvedFile unresolvedFile, SyntaxTree syntaxTree, ICompilation compilation, FoundReferenceCallback callback, CancellationToken cancellationToken ) : void
variable IVariable The variable for which to look.
unresolvedFile CSharpUnresolvedFile The type system representation of the file being searched.
syntaxTree SyntaxTree The syntax tree of the file being searched.
compilation ICompilation The compilation.
callback FoundReferenceCallback Callback used to report the references that were found.
cancellationToken System.Threading.CancellationToken Cancellation token that may be used to cancel the operation.
Résultat void
		public void FindLocalReferences(IVariable variable, CSharpUnresolvedFile unresolvedFile, SyntaxTree syntaxTree,
		                                ICompilation compilation, FoundReferenceCallback callback, CancellationToken cancellationToken)
		{
			if (variable == null)
				throw new ArgumentNullException("variable");
			var searchScope = new SearchScope(c => new FindLocalReferencesNavigator(variable));
			searchScope.declarationCompilation = compilation;
			FindReferencesInFile(searchScope, unresolvedFile, syntaxTree, compilation, callback, cancellationToken);
		}
		

Usage Example

        public FindUsagesResponse FindUsages(FindUsagesRequest request)
        {
            var res = _parser.ParsedContent(request.Buffer, request.FileName);
            var loc = new TextLocation(request.Line, request.Column);
            var result = new ConcurrentBag<AstNode>();
            var findReferences = new FindReferences();
            ResolveResult resolveResult = ResolveAtLocation.Resolve(res.Compilation, res.UnresolvedFile, res.SyntaxTree, loc);
            if (resolveResult is LocalResolveResult)
            {
                var variable = (resolveResult as LocalResolveResult).Variable;
                findReferences.FindLocalReferences(variable, res.UnresolvedFile, res.SyntaxTree, res.Compilation, (node, rr) => result.Add(node), CancellationToken.None);
            }
            else
            {
                IEntity entity = null;
                if (resolveResult is TypeResolveResult)
                {
                    entity = (resolveResult as TypeResolveResult).Type.GetDefinition();
                }

                if (resolveResult is MemberResolveResult)
                {
                    entity = (resolveResult as MemberResolveResult).Member;
                }

                if (entity == null)
                {
                    return new FindUsagesResponse {Usages = new List<Usage>()};
                }
                var searchScopes = findReferences.GetSearchScopes(entity);

                var interesting = new List<CSharpUnresolvedFile>();

                foreach (var scope in searchScopes)
                {
                    var scopeInteresting = findReferences.GetInterestingFiles(scope, res.Compilation);
                    interesting.AddRange(scopeInteresting);
                }

                Parallel.ForEach(interesting, file =>
                    {
                        ParsedResult parsedResult = _parser.ParsedContent(
                            _solution.GetFile(file.FileName).Content.Text, file.FileName);
                        findReferences.FindReferencesInFile(searchScopes, file, parsedResult.SyntaxTree,
                                                            parsedResult.Compilation,
                                                            (node, rr) => result.Add(node), CancellationToken.None);
                    });

            }

            var usages = result.Select(node => new Usage
            {
                FileName = node.GetRegion().FileName,
                Text = node.Preview(_solution.GetFile(node.GetRegion().FileName)).Replace("'", "''"),
                Line = node.StartLocation.Line,
                Column = node.StartLocation.Column,
            });

            return new FindUsagesResponse { Usages = usages };
        }
All Usage Examples Of ICSharpCode.NRefactory.CSharp.Resolver.FindReferences::FindLocalReferences