2013-01-12 54 views
2

我使用下面的代碼:如何將ComObject投射到ENVDTE.Project?

Object projObj = htProjects[selectedNode]; // htProjects is a Hashtable:key-project Name,value-ENVDTE.Project 
Project selectedProject = (Project)projObj; 

我收到以下錯誤:

Unable to cast COM object of type 'System.__ComObject' to interface type 'EnvDTE.Project'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{866311E6-C887-4143-9833-645F5B93F6F1}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)). 

我嘗試註冊DLL提到here但似乎有一些其他的問題。

回答

5

找到解決方案。一些散列表項具有ENVDTE.ProjectItems作爲值。所以需要檢查。

代碼:

 if (projObj is Project) 
     { 
      Project selectedProject = (Project)projObj; 
      MessageBox.Show(selectedProject.FullName); 
     } 
     else if(projObj is ProjectItem) 
     { 
      ProjectItem selectedProject = (ProjectItem)projObj; 
      MessageBox.Show(selectedProject.get_FileNames(1)); 
     }