簡體   English   中英

對於未建模的項目,如何將ComObject轉換為ENVDTE.Project?

[英]How to cast ComObject to ENVDTE.Project for Unmodeled projects?

我的問題與這一問題非常相似: 如何將ComObject轉換為ENVDTE.Project?

我要處理在Visual Studio->解決方案資源管理器中選擇的項目項。 如果加載了項目,則代碼可以正常工作,但我對卸載的項目有麻煩(它們稱為未建模的項目( http://msdn.microsoft.com/zh-cn/library/hw7ek4f4%28v=vs.80%29.aspx ) 。

為加載的項目鑄造所選項目uiItem.Object是EnvDTE.Project很好,但是如何鑄造未建模的項目? 沒有“ UnmodeledProject ”類,強制轉換uiItem.Object為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
            {  ...

由於找不到這種情況的解決方案,因此使用了以下技巧:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM