[英]Roslyn Get method invocation references in solution
我使用.NET Framework 4.6创建了一个非常简单的解决方案,其中包含两个项目
DataRepository.cs
using System.Collections.Generic;
using System.Linq;
namespace TestReferences.Data
{
public class DataRepository
{
public IEnumerable<string> GetProductNames()
{
return Enumerable.Repeat("Prod Name", 30);
}
public IEnumerable<int> GetProductIds()
{
return Enumerable.Range(12, 13);
}
}
}
ProductService.cs
using System.Collections.Generic;
namespace TestReferences.Data
{
public class ProductService : IProductService
{
private readonly DataRepository dataRepository;
public ProductService()
{
dataRepository = new DataRepository();
}
public IEnumerable<string> GetRecentProductNames()
{
return this.dataRepository.GetProductNames();
}
public IEnumerable<int> GetRecentProductIds()
{
return this.dataRepository.GetProductIds();
}
}
}
IProductService.cs
using System.Collections.Generic;
namespace TestReferences.Data
{
public interface IProductService
{
IEnumerable<int> GetRecentProductIds();
IEnumerable<string> GetRecentProductNames();
}
}
引用了TestReference.Data.dll的MVC项目
HomeController.cs
using System.Web.Mvc;
using TestReferences.Data;
namespace TestReferences.Web.Controllers
{
public class HomeController : Controller
{
IProductService productService;
public HomeController()
{
this.productService = new ProductService();
}
public ActionResult Index()
{
this.productService.GetRecentProductIds();
this.productService.GetRecentProductNames();
return View();
}
public ActionResult About()
{
this.productService.GetRecentProductNames();
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
在这种结构中,如果打开ProductService.cs
并将光标放在GetRecentProductNames
然后按shift + F12
键,则会在TestReference.Web
显示2个结果(Code-lens在界面中还显示了一个结果,仅适用于Enterprise版本)。 我创建了另一个控制台应用程序以获取相同的引用。
public static void Main()
{
MSBuildWorkspace ms = MSBuildWorkspace.Create();
Solution solution = ms.OpenSolutionAsync(@"D:\SampleApps\TestReferences\TestReferences.sln").Result;
Document doc = solution
.Projects
.Where(p => p.Name == "TestReferences.Data")
.SelectMany(p => p.Documents)
.FirstOrDefault(d => d.Name == "ProductService.cs");
if (doc == null)
{
throw new NullReferenceException("DOc");
}
SemanticModel model = doc.GetSemanticModelAsync().Result;
List<MethodDeclarationSyntax> methodDeclarations = doc.GetSyntaxRootAsync().Result.DescendantNodes().OfType<MethodDeclarationSyntax>().ToList();
// Method declaration is GetRecentProductNames()
MethodDeclarationSyntax m = methodDeclarations.First();
ISymbol symbolInfo = model.GetDeclaredSymbol(m);
IEnumerable<ReferencedSymbol> references = SymbolFinder.FindReferencesAsync(symbolInfo, doc.Project.Solution).Result;
}
我在references
收到两个项目,但它们的位置为0。
[0] | GetRecentProductNames,0个引用
[1] GetRecentProductNames,0个引用
这是我与Roslyn和Microsoft.CodeAnalysis
第一次接触。 库的版本是
<package id="Microsoft.CodeAnalysis" version="2.6.1" targetFramework="net46" />
GetDeclaredSymbol
返回关联的符号(如果其为声明语法节点)。 这不是您要找的东西。
您应该使用GetSymbolInfo
来获取有关语法节点的符号信息:
var symbolInfo = semanticModel.GetSymbolInfo(m);
var references = SymbolFinder.FindReferencesAsync(symbolInfo, doc.Project.Solution).Result;
实际上,我试图实现与嵌入在Visual Studio 2017中的“ View Call Hierarchy
”相同的功能。只需右键单击方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.