C# 遍历全局/局部范围中的所有EnvDTE.codelement

C# 遍历全局/局部范围中的所有EnvDTE.codelement,c#,C#,是否可以遍历给定源文件中的所有CodeElement对象 我真正的目标是能够从Visual Studio加载项中找出光标位置的变量类型。。我尝试了以下代码: // identify where the cursor is TextSelection selection = (TextSelection)_applicationObject.ActiveWindow.Selection; // get the starting point EditPoint Start = selection

是否可以遍历给定源文件中的所有CodeElement对象

我真正的目标是能够从Visual Studio加载项中找出光标位置的变量类型。。我尝试了以下代码:

// identify where the cursor is
TextSelection selection =
  (TextSelection)_applicationObject.ActiveWindow.Selection;
// get the starting point
EditPoint Start = selection.TopPoint.CreateEditPoint();
// get the element under the cursor
CodeElement element = _applicationObject.ActiveDocument.ProjectItem.
   FileCodeModel.CodeElementFromPoint(Start,
   vsCMElement.vsCMElementVariable);

if (element.Kind == vsCMElement.vsCMElementVariable)
{
    CodeVariable theVar = element as CodeVariable;
    string t = theVar.Type.AsString;
    MessageBox(t);
}
但它也有一些问题:

  • 它只在类的作用域中工作,不在函数作用域或全局作用域中工作(对于c++)

  • 它在光标位于声明位置时工作,我希望它在光标位于使用变量的位置时工作

那么,有没有办法在不构建自己的编译器的情况下获取这些语义信息呢