Search code examples
visual-studio-2015visual-studio-addins

VS 2015 Addin: Name and type of object at the current text point


I am trying to determine the name and the type (wheter the object is a method, property, etc.) of the element in which the caret is currently placed for a Visual Studio Plugin. I already tried to use CodeElements, but I'm just able to receive the elements declared in the current document and found no way to get the desired information.

Example:

Console.Wr|iteLine(); // Caret is placed after the 'r'

In above's short example, I want to receive "WriteLine" as the current object's name and I'd like to know that it is a method. Is there a simple way to do this? Any help is welcome.

Thanks in advance


Solution

  • I recommend to use Roslyn code analysis. See for example Create a typed variable from the current method invocation. It finds Microsoft.CodeAnalysis.IMethodSymbol from the current text point.