我正在为 resharper 编写一个插件,我想用它来从ConcreteCommand
->ConcreteCommandHandler
这些类型看起来像这样的地方导航
public class ConcreteCommand : ICommand
public class ConcreteCommandHandler : ICommandHandler<ConcreteCommand>
当光标位于实例/定义上时,我已经添加了导航菜单选项ICommand
(目前仅通过检查名称是否包含“Command”而不是“CommandHandler”),我认为我有实际所需的代码搜索一个继承某些东西的类型,但我的问题是我唯一真正拥有的类型是我的ConcereteCommand
,我需要创建(或获取对)泛型类型作为光标当前所在的类型ICommandHandler<T>
。T
所以我还有两件事我还想知道:
- 如何检查我
IDeclaredElement
的是否是特定接口的实现(理想情况下,通过在配置中的字符串中指定全名)? - 如何创建一个
ITypeElement
特定接口的泛型类型,我可以从现有IDeclaredElement
的 s 类型中设置泛型类型,然后我可以找到继承它的类?
我现有的代码如下所示:
[ContextNavigationProvider]
public class CommandHandlerNavigationProvider : INavigateFromHereProvider
{
public IEnumerable<ContextNavigation> CreateWorkflow(IDataContext dataContext)
{
ICollection<IDeclaredElement> declaredElements = dataContext.GetData(DataConstants.DECLARED_ELEMENTS);
if (declaredElements != null || declaredElements.Any())
{
IDeclaredElement declaredElement = declaredElements.First();
if (IsCommand(declaredElement))
{
var solution = dataContext.GetData(JetBrains.ProjectModel.DataContext.DataConstants.SOLUTION);
yield return new ContextNavigation("This Command's &handler", null, NavigationActionGroup.Other, () => { GotToInheritor(solution,declaredElement); });
}
}
}
private void GotToInheritor(ISolution solution, IDeclaredElement declaredElement)
{
var inheritorsConsumer = new InheritorsConsumer();
SearchDomainFactory searchDomainFactory = solution.GetComponent<SearchDomainFactory>();
//How can I create the ITypeElement MyNameSpace.ICommandHandler<(ITypeElement)declaredElement> here?
solution.GetPsiServices().Finder.FindInheritors((ITypeElement)declaredElement, searchDomainFactory.CreateSearchDomain(solution, true), inheritorsConsumer, NullProgressIndicator.Instance);
}
private bool IsCommand(IDeclaredElement declaredElement)
{
//How can I check if my declaredElement is an implementation of ICommand here?
string className = declaredElement.ShortName;
return className.Contains("Command")
&& !className.Contains("CommandHandler");
}
}