4

您如何以编程方式查找和迭代 Visual Studio 2010 解决方案中的所有项目和 dll 引用?

我可以迭代所有项目并找到该Project.ProjectItems属性,Project.Properties但没有找到任何引用引用的方法(双关语)。

这发生在加载项中,因此 DTE 解决方案比建议我们迭代文件的任何人都更可取。

根据以下答案提出的解决方案:

您需要找到并包含对 VSLangProj.dll 的引用(例如在 中Program Files\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies

然后您可以像这样迭代所有选定项目的项目和 DLL 引用

foreach (Project project in (object[])_applicationObject.ActiveSolutionProjects)
{
    VSProject vsProject = project.Object as VSProject;
    if (vsProject != null)
    {
        foreach (Reference reference in vsProject.References)
        {
              // Do cool stuff here
        }
    }
}

Tomas Lycken的相关资料:

_applicationObject 是我的加载项中的私有成员,例如

private DTE2 _applicationObject;

我在连接中设置它是这样的:

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
4

2 回答 2

3

C# and VB projects have an 'Object' property which you can cast into a VSProject, from which you can access the references. There's sample VB code on those pages.

于 2011-07-04T14:40:46.193 回答
3

You can access the VB/C# specific VSProject object from the Object property of the Project object. VSProject has a References property, through which you can drill down to the individual references.

It has to be this way, if you think about it, since not all projects loaded in visual studio will necessarily support references to .NET assemblies, so it has to be something specialized for C#/VB (and other .NET languages), rather than off of the core Project object.

于 2011-07-04T14:40:55.403 回答