Search code examples
c#.netdynamicvisual-studio-2015vsix

Unable to locate VSProject interface in VSLangProj


Edit: Note: I'm using Visual Studio 2015

I'm trying to write a VisualStudio Extension, using VSIX project, that presents all references of the currently loaded solution.

I'm able to get the current solution and the project in it (All projects, in fact) with the code below.

I separated the problemtic code to GetProjectReferences method.
I can't get to reference VSLangProj.VSProject.

 DTE2 dte2 = Package.GetGlobalService(typeof(DTE)) as     
 var sol = dte2.Solution;
 var projs = sol.Projects;

 foreach (Project project in projs)
 {
     if (project.Kind == ProjectKinds.vsProjectKindSolutionFolder)
     {   
         //Ignoring Solution Folders for now
     }
     else
     {
         //This method tries to get the refernces
         AppendProject(sb,project);
     }               
 }

 private void GetProjectReferences(StringBuilder sb, Project project)
    {            
        var vsProject = project.Object as VSLangProj.VSProject;
        var references = vsProject.References as VSLangProj.References;
        sb.AppendLine(project.Name + ": " + references.Count);
        int counter = 1;
        foreach (var reference in references)
        {
            sb.AppendLine((counter++)+": " + reference.Name + " (" + reference.Path +")");
        }
    }

It doesn't match microsoft's documentation, and I wasn't able to locate this interface in any of the other Microsoft's libraries (VSLangProj2 and VSLangProj80), mentioned here: https://msdn.microsoft.com/en-us/library/vslangproj.aspx

I also tried using VB, to test the code here, to no avail: https://msdn.microsoft.com/en-us/library/vslangproj.vsproject.aspx

' Macro Editor
' This example retrieves the VSProject object if the first project
' the solution is a Visual Basic or C# project. This routine assumes
' that the solution contains at least one project.
Imports VSLangProj
Sub VSProjectExample()
   Dim aProject As Project
   Dim aVSProject As VSProject

   aProject = DTE.Solution.Projects.Item(1)
   If (aProject.Kind = PrjKind.prjKindVBProject) _
   Or (aProject.Kind = PrjKind.prjKindCSharpProject) Then
      aVSProject = CType(DTE.Solution.Projects.Item(1).Object, VSProject)
      MsgBox(aVSProject.Project.FullName)
   Else
      MsgBox("The first project is not a Visual Basic or C# project.")
   End If
End Sub

Solution

  • Thanks to @ErikEJ, I was able to find what's missing: I didn't have the VSLangProj reference. It was in the Extensions tab in Reference Manager

    VSLangProj extension reference