使用Resharper SDK查找ITypeElement或IDeclaredElement的用法

时间:2019-03-26 08:50:20

标签: c# resharper resharper-sdk

我正在尝试使用Resharper SDK插件创建自定义导航到插件。当我站在自己的类型上时,已经设法获取了IDeclaredElement或ITypeElement

var referenceName = dataContext.GetSelectedTreeNode<IReferenceName>();
var declaration = referenceName?.Reference.Resolve()?.DeclaredElement as ITypeElement;
if (declaration != null)
{
    //TODO: Find all usages here and check if my type is used as single argument to a method (Visitor pattern)
}

SDK文档确实很稀疏,我在该主题上找不到任何东西。谢谢

1 个答案:

答案 0 :(得分:0)

经过反复试验,我找到了一个可行的解决方案。 IFinder.FindAllReferences

var foundMethods = declaration
    .GetPsiServices()
    .Finder
    .FindAllReferences(declaration)
    .Select(r => ((r.GetTreeNode().Parent as IUserTypeUsage)?
        .Parent as IRegularParameterDeclaration)?
        .Parent as IFormalParameterList)
    .Where(list => list != null && list.ParameterDeclarations.Count == 1)
    .Select(m => m.Parent as IMethodDeclaration)
    .Where(m => m != null)
    .ToList();

完整代码here