罗斯林/查找引用 - 无法正确加载工作区 [英] Roslyn / Find References - Can't properly load Workspace

查看:326
本文介绍了罗斯林/查找引用 - 无法正确加载工作区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写一些代码找到任何给定的方法所有的方法调用,因为我希望创建一个开源的UML序列图绘制工具。我有麻烦,但是,让过去的代码的前几行:/



出现的API已经大大改变了,我似乎无法推断正确使用。通过查看代码



当我做的:

  VAR工作区=新CustomWorkspace(); 
串solutionPath = @C:\Workspace\RoslynTest\RoslynTest.sln
VAR解决方案= workspace.CurrentSolution;



我觉得workspace.CurrentSolution有0个项目。我想这将是相当于什么以前 Workspace.LoadSolution(字符串solutionFile)那么这将理应包含在解决方案的任何项目,但我没有找到任何成功这条道路。



我是严重的混乱0.o



如果有人可以提供关于如何一些额外的指导意见我可以使用FindReferences API来识别特定方法的所有调用,这将是非常感谢!



另外,我会好起来采取静态分析方法?我想支持之类的东西lambda表达式,迭代方法和异步。



===================== ===============================================



编辑 -



下面是根据公认的答案一个完整的例子:

 使用System.Linq的; 
使用Microsoft.CodeAnalysis.CSharp;
使用Microsoft.CodeAnalysis.CSharp.Syntax;
使用Microsoft.CodeAnalysis.MSBuild;使用Microsoft.CodeAnalysis.FindSymbols
;使用System.Diagnostics程序
;

命名空间RoslynTest
{
类节目
{
静态无效的主要(字串[] args)
{
串solutionPath = @C:\Workspace\RoslynTest\RoslynTest.sln
无功工作区= MSBuildWorkspace.Create();
VAR解决方案= workspace.OpenSolutionAsync(solutionPath)。结果;
变种项目= solution.Projects.Where(p值=> p.Name ==RoslynTest)第一();
VAR编译= project.GetCompilationAsync()结果。
变种programClass = compilation.GetTypeByMetadataName(RoslynTest.Program);

VAR barMethod = programClass.GetMembers(酒吧)第一();
VAR fooMethod = programClass.GetMembers(富)第一()。

VAR barResult = SymbolFinder.FindReferencesAsync(barMethod,溶液).Result.ToList();
变种fooResult = SymbolFinder.FindReferencesAsync(fooMethod,溶液).Result.ToList();

Debug.Assert的(barResult.First()Locations.Count()== 1);
Debug.Assert的(fooResult.First()Locations.Count()== 0);
}

公共BOOL美孚()
{
返回酒吧==酒吧();
}

公共字符串栏()
{
返回BAR;
}
}
}


解决方案

CustomWorkspace




一个工作区,允许手动添加的项目和文档。




由于你试图加载一个解决方案,您应该使用 MSBuildWorkspace ,这是




这可以通过打开的MSBuild解决方案和项目文件中填充的工作空间。




您可以创建一个新的 MSBuildWorkspace 并调用的 OpenSolutionAsync solutionPath 。对于参考的发现的一部分,看看在 SymbolFinder


I'm trying to write some code to find all method invocations of any given method as I am looking to create an open source UML Sequence Diagramming tool. I'm having trouble, however, getting past the first few lines of code :/

The API appears to have changed drastically and I can't seem to infer proper usage by looking at the code.

When I do:

    var workspace = new CustomWorkspace();
    string solutionPath = @"C:\Workspace\RoslynTest\RoslynTest.sln";
    var solution = workspace.CurrentSolution;

I find that workspace.CurrentSolution has 0 Projects. I figured this would be the equivalent to what was previously Workspace.LoadSolution( string solutionFile ) which would then supposedly contain any Projects in the Solution, but I am not finding any success with this path.

I am terribly confused 0.o

If someone could offer some additional guidance as to how I can use the FindReferences API to identify all invocations of a particular method, it would be very much appreciated!

Alternatively, would I be better off taking a static-analysis approach? I would like to support things like lambdas, iterator methods and async.

====================================================================

Edit -

Here is a full example based on the accepted answer:

using System.Linq;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.CodeAnalysis.FindSymbols;
using System.Diagnostics;

namespace RoslynTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string solutionPath = @"C:\Workspace\RoslynTest\RoslynTest.sln";
            var workspace = MSBuildWorkspace.Create();
            var solution = workspace.OpenSolutionAsync(solutionPath).Result;
            var project = solution.Projects.Where(p => p.Name == "RoslynTest").First();
            var compilation = project.GetCompilationAsync().Result;
            var programClass = compilation.GetTypeByMetadataName("RoslynTest.Program");

            var barMethod = programClass.GetMembers("Bar").First();
            var fooMethod = programClass.GetMembers("Foo").First();

            var barResult = SymbolFinder.FindReferencesAsync(barMethod, solution).Result.ToList();
            var fooResult = SymbolFinder.FindReferencesAsync(fooMethod, solution).Result.ToList();

            Debug.Assert(barResult.First().Locations.Count() == 1);
            Debug.Assert(fooResult.First().Locations.Count() == 0);
        }

        public bool Foo()
        {
            return "Bar" == Bar();
        }

        public string Bar()
        {
            return "Bar";
        }
    }
}

解决方案

CustomWorkspace is

A workspace that allows manual addition of projects and documents.

Since you're trying to load a solution, you should use the MSBuildWorkspace, which is

A workspace that can be populated by opening MSBuild solution and project files.

You can create a new MSBuildWorkspace and call OpenSolutionAsync with your solutionPath. For the reference finding part, take a look at the SymbolFinder.

这篇关于罗斯林/查找引用 - 无法正确加载工作区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆