我的问题与这个问题非常相似:How to cast ComObject to ENVDTE.Project?
我想处理在 Visual Studio -> 解决方案资源管理器中选择的项目项。如果项目已加载,代码工作正常,但卸载项目有问题(它们被称为未建模项目(http://msdn.microsoft.com/en-us/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
{ ...