How to get access to the IType / IDeclaredType from an ITreeNode in ReSharper?

67 Views Asked by At

I am exploring writing Plugins for ReSharper / Rider. Following the documentation's example, I am building a context action for ReSharper.

Here is the context action, which is not doing much yet:

[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?
    }

    ...
}

enter image description here

Whenever I click on something in the source code (like a type as shown above), the constructor of my context action class is called. I have access to the name with the ITreeNode but I do not know how to move from the tree representation to the actual type representation used by ReSharper.

On clicked

Question

How can I to get a reference to the type (IType, IDeclaredType...) that has been clicked by the user?

1

There are 1 best solutions below

0
On BEST ANSWER

You need something like this:

var objectCreationExpression = dataProvider.GetSelectedElement<IObjectCreationExpression>();
      var typeUsage = objectCreationExpression?.TypeUsage;

      if (typeUsage != null)
      {
        var type = CSharpTypeFactory.CreateType(typeUsage);
      }