如何将ComObject转换为ENVDTE.未建模项目的项目

本文关键字:项目 建模 ENVDTE ComObject 转换 | 更新日期: 2023-09-27 18:16:45

我的问题与此非常相似:如何将ComObject转换为ENVDTE.Project?

我想处理在Visual Studio ->解决方案资源管理器中选择的项目项。如果项目加载代码工作正常,但我有卸载项目的麻烦(他们被称为unmodeling项目(http://msdn.microsoft.com/en-us/library/hw7ek4f4%28v=vs.80%29.aspx)。

为加载的项目铸造选中的项目uiItem。对象是EnvDTE。项目很好,但是如何cast未建模的项目?没有'UnmodeledProject'类和cast uiItem。Object is ProjectItem不工作

这是我的代码:

Window solutionExplorer = mApplicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer);
if(solutionExplorer != null)
{
    UIHierarchy uiHierarchy = (UIHierarchy)solutionExplorer.Object;
    if (uiHierarchy != null)
    {
        object[] selectedItems = (object[])uiHierarchy.SelectedItems;
        foreach (UIHierarchyItem uiItem in selectedItems)
        {                              
            // Valid project
            if (uiItem.Object is EnvDTE.Project)
            {
                EnvDTE.Project project = uiItem.Object as EnvDTE.Project;
                if (project.FullName.Contains(".vdproj") || project.Kind == "{54435603-DBB4-11D2-8724-00A0C9A8B90C}")
                {
                }
            }
            else if (uiItem.Object is ProjectItem)
            {
              // This is never jumped...
            }
            else
            {  ...

如何将ComObject转换为ENVDTE.未建模项目的项目

由于我没有找到解决这种情况的方法,我使用了这个技巧:

string pathToVdProject = null;
try
{
    Window solutionExplorer = mApplicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer);
    if (solutionExplorer != null)
    {
        UIHierarchy uiHierarchy = (UIHierarchy)solutionExplorer.Object;
        if (uiHierarchy != null)
        {
            object[] selectedItems = (object[])uiHierarchy.SelectedItems;
            foreach (UIHierarchyItem uiItem in selectedItems)
            {
                // Valid project
                if (uiItem.Object is EnvDTE.Project)
                {
                    EnvDTE.Project project = uiItem.Object as EnvDTE.Project;
                    if (project.FullName.Contains(".vdproj") || project.UniqueName.Contains(".vdproj")
                        || (String.Compare(project.Kind, ProjectsGuids.guidVdSetupProject, true) == 0))                                    
                    {
                        // Valid Project has property FullName which is full path to .vdproj file
                        pathToVdProject = project.FullName;
                        break;
                    }
                }
                else if (uiItem.Object is ProjectItem)
                {
                    // This never happens...
                }
                else
                {
                    // This is a little tricky: Unmodeled Projects cannot be casted to EnvDTE.Project http://msdn.microsoft.com/en-us/library/hw7ek4f4%28v=vs.80%29.aspx 
                    Solution2 solution = (Solution2)mApplicationObject.Solution;
                    // So get all projects in solution (including unmodeled) and try to find a match by name
                    foreach (Project project in solution.Projects)
                    {
                        if (project.Kind == EnvDTE.Constants.vsProjectKindUnmodeled)
                        {
                            // Unmodeled project found (Normal projects are recognized in 'uiItem.Object is EnvDTE.Project'
                            if (project.Name.Contains(uiItem.Name))
                            {
                                // This is 'Project' for selected item
                                if (project.Name.Contains(".vdproj") || project.UniqueName.Contains(".vdproj"))
                                {
                                    // Unmodeled projects does not offer property FullName and UniqueName does NOT contain full path to file!
                                    FileInfo fileInfo = new FileInfo(solution.FullName);
                                    // Create full path from solution (.sln) path and project relative path
                                    pathToVdProject = fileInfo.DirectoryName + "''" + project.UniqueName;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

解决方案资源管理器中所有已加载/已卸载项目的列表将在您的EnvDTE应用程序对象中可用。没有使用解决方案资源管理器窗口和UIHierarchy,我得到了项目的详细信息。下面的代码片段对我来说很好。请检查一下它是否适合你。

For Each item As EnvDTE.Project In mApplicationObject.Solution.Projects
     If item.Globals Is Nothing AndAlso item.Object Is Nothing Then
        Console.WriteLine(item.Name + "  is currently unloaded!")
     End If
Next