C# 如何将未建模项目的ComObject强制转换为ENVDTE.Project?
我的问题与此非常相似: 我想处理在Visual Studio->解决方案资源管理器中选择的项目项。如果加载了项目,代码工作正常,但对于卸载的项目(它们被称为未建模项目())我有麻烦 为加载的项目强制转换选定项uiItem.Object是EnvDTE.Project可以,但是如何强制转换未建模的项目? 没有“未删除项目”类,强制转换uiItem.Object is ProjectItem不起作用 这是我的代码:C# 如何将未建模项目的ComObject强制转换为ENVDTE.Project?,c#,visual-studio-addins,envdte,C#,Visual Studio Addins,Envdte,我的问题与此非常相似: 我想处理在Visual Studio->解决方案资源管理器中选择的项目项。如果加载了项目,代码工作正常,但对于卸载的项目(它们被称为未建模项目())我有麻烦 为加载的项目强制转换选定项uiItem.Object是EnvDTE.Project可以,但是如何强制转换未建模的项目? 没有“未删除项目”类,强制转换uiItem.Object is ProjectItem不起作用 这是我的代码: Window solutionExplorer = mApplicationObjec
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
{ ...
解决方案资源管理器中所有加载/卸载项目的列表将在您的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
由于我没有找到解决这种情况的方法,我使用了以下技巧:
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;
}
}
}
}
}
}
}
}
不,这是我不想去的方式。我想识别所选项目的类别,但感谢您的评论。