编辑:注意:我正在使用Visual Studio 2015
我正在尝试使用VSIX项目编写VisualStudio扩展,它显示当前加载的解决方案的所有引用。
我能够使用下面的代码获取当前的解决方案和项目(事实上所有项目)。
我将问题代码分离为GetProjectReferences
方法
我无法参考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 +")");
}
}
它与microsoft的文档不匹配,我无法在任何其他Microsoft的库(VSLangProj2
和VSLangProj80
)中找到此接口,如下所述:
https://msdn.microsoft.com/en-us/library/vslangproj.aspx
我也试过用VB来测试代码,但无济于事: 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