Visual Studio 2008加载项检查层次结构项是否为解决方案文件夹

时间:2021-09-26 07:33:31

I've got a visual studio addin written by developer who is no longer at the company and have no idea how to debug it. But I want to add a feature so it can recurse into solution folders.

我有一个由开发人员编写的视觉工作室插件,他不在公司,也不知道如何调试它。但我想添加一个功能,以便它可以递归到解决方案文件夹中。

Sounds simple but I'm not sure the api allows testing for this? Well there's got to be a way because AnkhSVN and VisualSVN work fine with Solution Folders.

听起来很简单,但我不确定api是否允许对此进行测试?那么必须有一种方法,因为AnkhSVN和VisualSVN与解决方案文件夹一起正常工作。

* I'm reaching out for some help on this issue.

*我正在寻求关于这个问题的一些帮助。

Thanks

Notes

-We are using solution folders to hide "Dependency Projects" which are basically a list of project references that we probably don't care about in the particular solution and want to hide by default.

- 我们正在使用解决方案文件夹来隐藏“依赖项目”,它基本上是我们在特定解决方案中可能不关心的项目引用列表,并且默认情况下要隐藏。

public class Connect : IDTExtensibility2, IDTCommandTarget
{

public void GetProjectLocations(DTE2 dte)
{

UIHierarchy UIH = dte.ToolWindows.SolutionExplorer;

try
{
     UIHierarchyItem UIHItemd = UIH.UIHierarchyItems.Item(1);
}
catch (Exception E)
{
  Debug.Write(E);
}

UIHierarchyItem UIHItem = UIH.UIHierarchyItems.Item(1);//this looks suspect to me

// Iterate through first level nodes.
for (int i = 1; i <= UIHItem.UIHierarchyItems.Count; i++)
{
  Project TempGeneralProjObj = dte.Solution.Item(i);  

  if (TempGeneralProjObj.Kind == PrjKind.prjKindCSharpProject)
  {
  }

}

}

}

2 个解决方案

#1


So far from my tests it appears that solution folders will cast to type Project surprisingly and once that is done the Project.ProjectItems property will hold a list of Projects that may exists underneath that folder. So in short, this is one way to at least get information about how things are structured. The problem however is that each ProjectItem located underneath a solution folder appears to cast find to type ProjectItem but doesn't seem to be able to be cast to a Project.

到目前为止,在我的测试中,似乎解决方案文件夹将被强制转换为类型Project,一旦完成,Project.ProjectItems属性将保存该文件夹下可能存在的Projects列表。简而言之,这是至少获取有关事物结构的信息的一种方法。但问题是,位于解决方案文件夹下的每个ProjectItem似乎都会将find强制转换为类型ProjectItem,但似乎无法强制转换为Project。

This is how I'm currently detecting a solution folder in my loop.

这就是我目前在循环中检测解决方案文件夹的方式。

if(project.Kind == "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}")
{
    // TODO: Do your thing
}

This has also been frustrating me and I've also noticed a bug in how ActiveReports handles solution folders as well which is related to this same issue.

这也令我感到沮丧,我也注意到ActiveReports如何处理解决方案文件夹的错误,这与同一问题有关。

UPDATE!

Ok so I found the solution but I can't claim it 100% because I found most of it at Macaw's Blog.

好的,所以我找到了解决方案,但我不能100%声称它,因为我在Macaw的Blog上发现了大部分内容。

So it appears that my original findings were right on however in order to get the actual project type for each ProjectItem under the solution item you need to look under the ProjectItem.SubProject property.

因此,我的原始发现似乎是正确的,以便在解决方案项下获取每个ProjectItem的实际项目类型,您需要在ProjectItem.SubProject属性下查看。

Now Macaw takes a recursive approach to walking the project structure which I think I would also normally recommend however in my case I wanted a single method implementation to simply log out an XML representation of the project for simple research purposes so I ended up using a Stack implementation. For reference you can find my code below which is successfully handling at least one level of solution folders full of projects only and no other specialty solution items.

现在,Macaw采用递归方式来处理项目结构,我认为我通常也会建议这样做。但是在我的情况下,我希望单个方法实现只是为了简单的研究目的而简单地注销项目的XML表示,所以我最终使用了一个堆栈实现。作为参考,您可以在下面找到我的代码,该代码成功处理至少一个级别的解决方案文件夹,仅包含项目而没有其他专业解决方案项目。

        XElement rootNode = new XElement("Solution");
        rootNode.Add(new XAttribute("Name", _applicationObject.Solution.FullName));

        Stack<Project> projectStack = 
            new Stack<Project>(_applicationObject.Solution.Projects.Cast<Project>());

        while(projectStack.Count > 0)
        {
            var project = (Project)projectStack.Pop();
            var solutionItemName = "Project";

            if(project.Kind == "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}")
            {
                foreach(ProjectItem innerProject in project.ProjectItems)
                {
                    if(innerProject.SubProject != null)
                    {
                        projectStack.Push(innerProject.SubProject);
                    }
                }
                solutionItemName = "Folder";
            }

            var projectNode = new XElement(
                solutionItemName, 
                new XAttribute("Name", project.Name),
                new XAttribute("Kind", project.Kind)
                );
            rootNode.Add(projectNode);

            foreach(ProjectItem item in project.ProjectItems)
            {
                var itemNode = new XElement("Item", new XAttribute("Name", item.Name));
                projectNode.Add(itemNode);

                if(item.Properties == null)
                {
                    continue;
                }

                foreach(Property property in item.Properties)
                {
                    var propertyNode = new XElement(property.Name, property.Value);
                    itemNode.Add(propertyNode);
                }
            }
        }

By the fact of this post and by apparent bugs in other Add-ins it is apparent that this isn't the most intuitive design but thats what we have to live with.

根据这篇文章的事实以及其他插件中的明显错误,很明显这不是最直观的设计,但这就是我们必须忍受的。

#2


To debug a Visual Studio add-in, load the source code into a copy of visual studio that is not running the add-in. Then, configure the project to start a second copy of visual studio when you "run" the project, that second copy will then run with the first able to breakpoint and debug it.

若要调试Visual Studio加载项,请将源代码加载到未运行加载项的visual studio副本中。然后,配置项目以在“运行”项目时启动Visual Studio的第二个副本,然后第二个副本将运行第一个能够断点并调试它。

Make sure you have a batch file (or equivalent) to clean up, so that you can always get back to running VS without the plugin.

确保您有一个批处理文件(或等效文件)来清理,这样您就可以在没有插件的情况下返回运行VS.

Useful resources ...

有用的资源......

#1


So far from my tests it appears that solution folders will cast to type Project surprisingly and once that is done the Project.ProjectItems property will hold a list of Projects that may exists underneath that folder. So in short, this is one way to at least get information about how things are structured. The problem however is that each ProjectItem located underneath a solution folder appears to cast find to type ProjectItem but doesn't seem to be able to be cast to a Project.

到目前为止,在我的测试中,似乎解决方案文件夹将被强制转换为类型Project,一旦完成,Project.ProjectItems属性将保存该文件夹下可能存在的Projects列表。简而言之,这是至少获取有关事物结构的信息的一种方法。但问题是,位于解决方案文件夹下的每个ProjectItem似乎都会将find强制转换为类型ProjectItem,但似乎无法强制转换为Project。

This is how I'm currently detecting a solution folder in my loop.

这就是我目前在循环中检测解决方案文件夹的方式。

if(project.Kind == "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}")
{
    // TODO: Do your thing
}

This has also been frustrating me and I've also noticed a bug in how ActiveReports handles solution folders as well which is related to this same issue.

这也令我感到沮丧,我也注意到ActiveReports如何处理解决方案文件夹的错误,这与同一问题有关。

UPDATE!

Ok so I found the solution but I can't claim it 100% because I found most of it at Macaw's Blog.

好的,所以我找到了解决方案,但我不能100%声称它,因为我在Macaw的Blog上发现了大部分内容。

So it appears that my original findings were right on however in order to get the actual project type for each ProjectItem under the solution item you need to look under the ProjectItem.SubProject property.

因此,我的原始发现似乎是正确的,以便在解决方案项下获取每个ProjectItem的实际项目类型,您需要在ProjectItem.SubProject属性下查看。

Now Macaw takes a recursive approach to walking the project structure which I think I would also normally recommend however in my case I wanted a single method implementation to simply log out an XML representation of the project for simple research purposes so I ended up using a Stack implementation. For reference you can find my code below which is successfully handling at least one level of solution folders full of projects only and no other specialty solution items.

现在,Macaw采用递归方式来处理项目结构,我认为我通常也会建议这样做。但是在我的情况下,我希望单个方法实现只是为了简单的研究目的而简单地注销项目的XML表示,所以我最终使用了一个堆栈实现。作为参考,您可以在下面找到我的代码,该代码成功处理至少一个级别的解决方案文件夹,仅包含项目而没有其他专业解决方案项目。

        XElement rootNode = new XElement("Solution");
        rootNode.Add(new XAttribute("Name", _applicationObject.Solution.FullName));

        Stack<Project> projectStack = 
            new Stack<Project>(_applicationObject.Solution.Projects.Cast<Project>());

        while(projectStack.Count > 0)
        {
            var project = (Project)projectStack.Pop();
            var solutionItemName = "Project";

            if(project.Kind == "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}")
            {
                foreach(ProjectItem innerProject in project.ProjectItems)
                {
                    if(innerProject.SubProject != null)
                    {
                        projectStack.Push(innerProject.SubProject);
                    }
                }
                solutionItemName = "Folder";
            }

            var projectNode = new XElement(
                solutionItemName, 
                new XAttribute("Name", project.Name),
                new XAttribute("Kind", project.Kind)
                );
            rootNode.Add(projectNode);

            foreach(ProjectItem item in project.ProjectItems)
            {
                var itemNode = new XElement("Item", new XAttribute("Name", item.Name));
                projectNode.Add(itemNode);

                if(item.Properties == null)
                {
                    continue;
                }

                foreach(Property property in item.Properties)
                {
                    var propertyNode = new XElement(property.Name, property.Value);
                    itemNode.Add(propertyNode);
                }
            }
        }

By the fact of this post and by apparent bugs in other Add-ins it is apparent that this isn't the most intuitive design but thats what we have to live with.

根据这篇文章的事实以及其他插件中的明显错误,很明显这不是最直观的设计,但这就是我们必须忍受的。

#2


To debug a Visual Studio add-in, load the source code into a copy of visual studio that is not running the add-in. Then, configure the project to start a second copy of visual studio when you "run" the project, that second copy will then run with the first able to breakpoint and debug it.

若要调试Visual Studio加载项,请将源代码加载到未运行加载项的visual studio副本中。然后,配置项目以在“运行”项目时启动Visual Studio的第二个副本,然后第二个副本将运行第一个能够断点并调试它。

Make sure you have a batch file (or equivalent) to clean up, so that you can always get back to running VS without the plugin.

确保您有一个批处理文件(或等效文件)来清理,这样您就可以在没有插件的情况下返回运行VS.

Useful resources ...

有用的资源......