查找所有引用与罗斯林的方法 [英] Finding all references to a method with Roslyn

查看:339
本文介绍了查找所有引用与罗斯林的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待扫描一组的.cs文件,看看哪些调用的可空<的财产; T> (找到的所有引用)。例如,这将匹配:

 类节目
{
静态无效的主要()
{
诠释?可为空值= 123;
int值= nullable.Value;
}
}



我发现了罗斯林,看着一些样品,但其中不少是过时和API是巨大的。我怎么会去这样做呢?



我分析语法树后卡住了。这是我到目前为止有:

 公共静态无效分析(字符串源代码)
{
变种树= CSharpSyntaxTree.ParseText(源代码);

tree./*?善有善报吗? * /
}


解决方案

您很可能寻找 SymbolFinder 类并专门 FindAllReferences 方法。



这听起来像你有一些麻烦熟悉的罗斯林。我有一系列的博客文章,以帮助人们介绍了罗斯林称为了解罗斯林即时



由于@SLaks提到你将需要访问我在的第7部分:介绍语义模型



下面是说明你的API如何使用一个样本。如果你能,我会使用 MSBuildWorkspace 并从磁盘加载的项目,而不是在一个 AdHocWorkspace 像这样

  VAR的mscorlib = PortableExecutableReference.CreateFromAssembly(typeof运算(对象).Assembly); 
变种WS =新AdhocWorkspace();
//创建一个新的解决方案
VAR固体= SolutionId.CreateNewId();
VAR solutionInfo = SolutionInfo.Create(固体,VersionStamp.Create());
//创建新项目
VAR项目= ws.AddProject(样本,C#);
项目= project.AddMetadataReference(mscorlib程序);
//添加项目工作区
ws.TryApplyChanges(project.Solution);
字符串文本= @
类C
{
无效M()
{
M();
M();
}
};
VAR sourceText = SourceText.From(文本);
//创建新的文档
VAR DOC = ws.AddDocument(project.Id,NewDoc,sourceText);
//获取语义模型
VAR模型= doc.GetSemanticModelAsync()结果。
//获取第一次调用到M()
变种的MethodInvocation = doc.GetSyntaxRootAsync()Result.DescendantNodes()OfType&所述语法节点; InvocationExpressionSyntax方式>()第一();
VAR methodSymbol = model.GetSymbolInfo(的MethodInvocation).Symbol;
//查找到M()
VAR referencesToM = SymbolFinder.FindReferencesAsync(methodSymbol,doc.Project.Solution)。结果所有引用;


I'm looking to scan a group of .cs files to see which ones call the Value property of a Nullable<T> (finding all references). For example, this would match:

class Program
{
    static void Main()
    {
        int? nullable = 123;
        int value = nullable.Value;
    }
}

I found out about Roslyn and looked at some of the samples, but many of them are outdated and the API is huge. How would I go about doing this?

I'm stuck after parsing the syntax tree. This is what I have so far:

public static void Analyze(string sourceCode)
{
    var tree = CSharpSyntaxTree.ParseText(sourceCode);

    tree./* ??? What goes here? */
}

解决方案

You're probably looking for the SymbolFinder class and specifically the FindAllReferences method.

It sounds like you're having some trouble getting familiar with Roslyn. I've got a series of blog posts to help people get introduced to Roslyn called Learn Roslyn Now.

As @SLaks mentions you're going to need access to the semantic model which I cover in Part 7: Introduction to the Semantic Model

Here's a sample that shows you how the API can be used. If you're able to, I'd use MSBuildWorkspace and load the project from disk instead of creating it in an AdHocWorkspace like this.

var mscorlib = PortableExecutableReference.CreateFromAssembly(typeof(object).Assembly);
var ws = new AdhocWorkspace();
//Create new solution
var solId = SolutionId.CreateNewId();
var solutionInfo = SolutionInfo.Create(solId, VersionStamp.Create());
//Create new project
var project = ws.AddProject("Sample", "C#");
project = project.AddMetadataReference(mscorlib);
//Add project to workspace
ws.TryApplyChanges(project.Solution);
string text = @"
class C
{
    void M()
    {
        M();
        M();
    }
}";
var sourceText = SourceText.From(text);
//Create new document
var doc = ws.AddDocument(project.Id, "NewDoc", sourceText);
//Get the semantic model
var model = doc.GetSemanticModelAsync().Result;
//Get the syntax node for the first invocation to M()
var methodInvocation = doc.GetSyntaxRootAsync().Result.DescendantNodes().OfType<InvocationExpressionSyntax>().First();
var methodSymbol = model.GetSymbolInfo(methodInvocation).Symbol;
//Finds all references to M()
var referencesToM = SymbolFinder.FindReferencesAsync(methodSymbol,  doc.Project.Solution).Result;

这篇关于查找所有引用与罗斯林的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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