APISampleUnitTestsCS.FAQ.FindAllReferencesToAMethodInASolution C# (CSharp) Метод

FindAllReferencesToAMethodInASolution() приватный Метод

private FindAllReferencesToAMethodInASolution ( ) : void
Результат void
        public void FindAllReferencesToAMethodInASolution()
        {
            var source1 = @"
            namespace NS
            {
            public class C
            {
            public void MethodThatWeAreTryingToFind()
            {
            }
            public void AnotherMethod()
            {
            MethodThatWeAreTryingToFind(); // First Reference.
            }
            }
            }";
            var source2 = @"
            using NS;
            using Alias=NS.C;
            class Program
            {
            static void Main()
            {
            var c1 = new C();
            c1.MethodThatWeAreTryingToFind(); // Second Reference.
            c1.AnotherMethod();
            var c2 = new Alias();
            c2.MethodThatWeAreTryingToFind(); // Third Reference.
            }
            }";
            var solutionId = SolutionId.CreateNewId();
            ProjectId project1Id, project2Id;
            DocumentId document1Id, document2Id;
            var mscorlib = MetadataReference.CreateAssemblyReference("mscorlib");
            var solution = Solution.Create(solutionId)
                .AddCSharpProject("Project1", "Project1", out project1Id)
                .AddMetadataReference(project1Id, mscorlib)
                .AddDocument(project1Id, "File1.cs", source1, out document1Id)
                .UpdateCompilationOptions(project1Id,
                    new CompilationOptions(OutputKind.DynamicallyLinkedLibrary))
                .AddCSharpProject("Project2", "Project2", out project2Id)
                .AddMetadataReference(project2Id, mscorlib)
                .AddProjectReference(project2Id, project1Id)
                .AddDocument(project2Id, "File2.cs", source2, out document2Id);

            // If you wish to try against a real solution you could use code like
            // var solution = Solution.Load("<Path>");
            // OR var solution = Workspace.LoadSolution("<Path>").CurrentSolution;

            var project1 = solution.GetProject(project1Id);
            var document1 = project1.GetDocument(document1Id);

            // Get MethodDeclarationSyntax corresponding to the 'MethodThatWeAreTryingToFind'.
            MethodDeclarationSyntax methodDeclaration = document1.GetSyntaxRoot()
                .DescendantNodes().OfType<MethodDeclarationSyntax>()
                .Single(m => m.Identifier.ValueText == "MethodThatWeAreTryingToFind");

            // Get MethodSymbol corresponding to the 'MethodThatWeAreTryingToFind'.
            var method = (MethodSymbol)document1.GetSemanticModel()
                .GetDeclaredSymbol(methodDeclaration);

            // Find all references to the 'MethodThatWeAreTryingToFind' in the solution.
            IEnumerable<ReferencedSymbol> methodReferences = method.FindReferences(solution);
            Assert.AreEqual(1, methodReferences.Count());
            ReferencedSymbol methodReference = methodReferences.Single();
            Assert.AreEqual(3, methodReference.Locations.Count());

            var methodDefinition = (MethodSymbol)methodReference.Definition;
            Assert.AreEqual("MethodThatWeAreTryingToFind", methodDefinition.Name);
            Assert.IsTrue(methodReference.Definition.Locations.Single().IsInSource);
            Assert.AreEqual("File1.cs", methodReference.Definition.Locations.Single().SourceTree.FilePath);

            Assert.IsTrue(methodReference.Locations
                .All(referenceLocation => referenceLocation.Location.IsInSource));
            Assert.AreEqual(1, methodReference.Locations
                .Count(referenceLocation => referenceLocation.Document.Name == "File1.cs"));
            Assert.AreEqual(2, methodReference.Locations
                .Count(referenceLocation => referenceLocation.Document.Name == "File2.cs"));
        }