0

I'm trying to build some kind of custom Configuration Manager for Visual Studio 2013. I've created a VSPackage MenuCommand and currently run it in an visual studio experimental instance. To get the projects of the current solution i use this:

public static EnvDTE80.DTE2 GetActiveIDE()
{
    // Get an instance of currently running Visual Studio IDE.
    var dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.12.0");      
    return dte2;
}

public static IList<Project> Projects()
{
    Projects projects = GetActiveIDE().Solution.Projects;
    List<Project> list = new List<Project>();
    var item = projects.GetEnumerator();
    while (item.MoveNext())
    {
        var project = item.Current as Project;
        if (project == null)
        {
            continue;
        }
        list.Add(project);
    }

    return list;
}

When I try to access the ConfigurationManager of a project, I get a NullRefernceException:

var projects = Projects().OrderBy(p => p.Name).ToList();
foreach (var project in projects)
{
    DataRow row = solutionConfigurationData.NewRow();
    row[0] = project.Name;
    row[1] = project.ConfigurationManager.ActiveConfiguration.ConfigurationName;
    row[2] = project.ConfigurationManager.ActiveConfiguration.PlatformName;
}

The COM-Object itself (project) works fine, because if comment out row[1] and row[2] I get a list of all project names.

Don't know how else to get the project configuration, because all the posts i found use the configuration manager.

boFFeL
  • 31
  • 5

1 Answers1

0

1) Do not use Marshal.GetActiveObject to get the EnvDTE instance where your extension is loaded, it could return another running instance. Use GetService(typeof(EnvDTE.DTE))

2) Navigate the projects of the solution recursively, not linearly. See HOWTO: Navigate the files of a solution from a Visual Studio .NET macro or add-in.

3) EnvDTE.Project.ConfigurationManager can return null if the project doesn't support configuration. For C# and VB.NET project it should work, though. C++ projects use a different project model (VCConfiguration). For other projects maybe don't even support configuration programmatically.

Carlos Quintero
  • 4,300
  • 1
  • 11
  • 18