简体   繁体   English

C#从DTE获取配置

[英]C# Get Configuration from DTE

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. 我正在尝试为Visual Studio 2013构建某种自定义配置管理器。我创建了VSPackage MenuCommand,当前在Visual Studio实验实例中运行它。 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: 当我尝试访问项目的ConfigurationManager时,出现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. COM对象本身(项目)可以正常工作,因为如果注释掉row [1]和row [2],我会得到所有项目名称的列表。

Don't know how else to get the project configuration, because all the posts i found use the configuration manager. 不知道如何获取项目配置,因为我发现的所有帖子都使用配置管理器。

1) Do not use Marshal.GetActiveObject to get the EnvDTE instance where your extension is loaded, it could return another running instance. 1)不要使用Marshal.GetActiveObject获取扩展加载到的EnvDTE实例,它可能会返回另一个正在运行的实例。 Use GetService(typeof(EnvDTE.DTE)) 使用GetService(typeof(EnvDTE.DTE))

2) Navigate the projects of the solution recursively, not linearly. 2)递归地而不是线性地导航解决方案的项目。 See HOWTO: Navigate the files of a solution from a Visual Studio .NET macro or add-in . 请参见HOWTO: 从Visual Studio .NET宏或加载项中导航解决方案的文件

3) EnvDTE.Project.ConfigurationManager can return null if the project doesn't support configuration. 3)如果项目不支持配置,则EnvDTE.Project.ConfigurationManager可以返回null。 For C# and VB.NET project it should work, though. 对于C#和VB.NET项目,它应该可以工作。 C++ projects use a different project model ( VCConfiguration ). C ++项目使用不同的项目模型( VCConfiguration )。 For other projects maybe don't even support configuration programmatically. 对于其他项目,可能甚至不以编程方式支持配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM