【问题标题】:How to get access to the IType / IDeclaredType from an ITreeNode in ReSharper?如何从 ReSharper 中的 ITreeNode 访问 IType / IDeclaredType?
【发布时间】:2022-01-23 17:24:44
【问题描述】:

我正在探索为 ReSharper / Rider 编写插件。按照文档的example,我正在为 ReSharper 构建一个上下文操作。

这里是上下文动作,它还没有做太多:

[ContextAction
(
    Name = "New context action",
    Description = "Some description...",
    Group = "C#",
    Disabled = false,
    Priority = 1
)]
public sealed class NewContextAction : ContextActionBase
{
    public override string Text => "New context action";

    public NewContextAction(LanguageIndependentContextActionDataProvider dataProvider)
    {
        var selectedTreeNode = dataProvider.GetSelectedElement<ITreeNode>();

        // --> How can I get a reference to the type?
    }

    ...
}

每当我单击源代码中的某些内容(如上图所示的类型)时,都会调用上下文操作类的构造函数。我可以使用 ITreeNode 访问名称,但我不知道如何从树表示移动到 ReSharper 使用的实际类型表示。

问题

如何获取用户点击的类型(ITypeIDeclaredType...)的引用?

【问题讨论】:

    标签: c# resharper resharper-plugins resharper-sdk


    【解决方案1】:

    你需要这样的东西:

    var objectCreationExpression = dataProvider.GetSelectedElement<IObjectCreationExpression>();
          var typeUsage = objectCreationExpression?.TypeUsage;
    
          if (typeUsage != null)
          {
            var type = CSharpTypeFactory.CreateType(typeUsage);
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-08
      • 2012-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多